Iteration optimization

Note: my previous pull request is also included in this one - I can rebase on your master if you decide to pull them in separately (if at all)