Separate output generation from the loop iteration logic.
Merge pull request #84 from Dmitry-Me/separategeneration

Separate output generation from the loop iteration logic.