Made the rotations more robust and added comments