added convergence test and while loop to algorithm