reduce number of for loops