Performance optimisation: Merge two for loops into one.