Refactor out main function