Performance improvements to the algorithm.