Rotate algorithm