Added possibility to limit number of iterations.