Convert several while loops to for loops.
Merge pull request #10 from agouge/refactor/convert-to-for-loops

Convert several while loops to for loops.

This is cleaner, reduces the scope of the iterator and avoids confusion.