Improve the algorithm