Minor modification to the algorithm