cleanups and refactoring for main