Fix hoisting issue with function literal in loop