Added more efficient code for rotation