Combined similar rotation code into a single function.