completed

completed version of the algorithm