Rotation algorithm