Fixed rotating function, small refactoring