Turn main function into library
Move library portioni into repo root