first bulk of refactoring
cleaned up main