improve use of for loops