Added a limit to the number of iterations