Iterating over collections and writing lines