Refactor code to be more memory efficient

The pluginContext doesn't need to be created every time the plugin is applied. It's sufficient to create it when the plugin is defined for the first time.