optimize for loop