Convert several iterator while loops to for loops.

This removes confusion, limits scope and avoids bugs.
Merge pull request #36 from agouge/refactor/convert-to-for-loops

Convert several iterator while loops to for loops