Added check on maximum number of iterations.