minor improvement of the algorithm