algorithm is now iterative