fix issue with loop scope persistence in registering callback functions that depends on the current loop iteration state