bit of refactoring, taking as much logic out of main as possible