implemented rotate algorithm