Encapsulate main function

Moved function into a callable method