move most of the logic out of the plugin to increase testablility