avoid creating functions in loop