modified the algorithm