Various fixes, removed the bulk of main.cpp.