Refactoring rotation code