Move file io to inner loop conditional.
Add output for every iteration.