finish the algorithm