Restructure plugin code to be more testable