Refactored code structure

Moved most functions into their own source files in order to clean up
the main source file.