Most of main moved/refactored into two new function files