optimize for loops