Memory management refactor

Refactored TPG Module / C++ plugin code so it used reference counting to properly manage memory usage.