Extract functions out of for loops