Optimize for loops