Minor cleanup of main module, separated several things into their own modules, fewer global variables.