fixing issues around scope when being invoked in a different context