Rotate algorithm and test