polish the algorithm