Optimization to structure rotation algorithm.