It seems to me that you could just calcuate atan2(rawY, rawX) to get the rotation around the Z axis which is more directly what you want.  That would also save two sqrt() calls and one atan2 call.  I haven't tested that, though, and maybe it doesn't work.