collapse two for loops