Trying to improve the performance of the algorithm.