Refactor image rotation code so easier to follow