Fix closures created in loops behaving weirdly