Optimizing the rotate() function