Organized main.cpp further

Put more functions into header files to make main.cpp easier to read and
manage