refactored rotate method