Some cleanup of source and major restructuring of main.cpp