Fix issue attaching events in a loop.