This fixes error "Using $this when not in object context"
Nested functions are not part of the object and therefore don't have
access to the object instance $this!
This also fixes an error where calling collectData multiple times would
result in an attempt to redeclare all nested functions (which would
cause errors)