fix incorrect binding of closure variables in loop