Unnest loops

Reusing the iterator in a nested loop is not a good idea. And in
this case it is better to not have nested loops at all, because
we only have to create one array.
Merge pull request #1370 from ahocevar/unnest-loops

Unnest loops