HTC Vive camera rotation UpsideDown

I have a body following the rotation of the HMD/Camera, which works fine. Except for when I start looking downwards all the way to being basically upside down so my yaw is getting flipped. Which makes my body flip on the yaw axis. How would I go about to prevent this? Maybe a formula in relation to the pitch?