Avoid creating handler functions in loop