added convergence condition to the code