Encapsulated the functionality of the code into a service class which the plugin now makes use of. As a result the code became testable and there were tests added for the service and the patterns.