Added ability to set a maximum number of iterations.