Optimize algorithm.