Refactoring to support library use

Refactored out the main module call