Refactor rotate_state