refactored cos and sin code