Refactored a little bit to avoid multiple underscore wrappings

To avoid the generation of multiple underscore wrappers in loops, one
that can be referenced is created in the outer scope.