refactoring in the _rotate method to remove usage of goto