Refactored out some common code

Will need to change the imports later when using as proper module