fixed scope of $i inside of a closure