finished the algorithm