Refactored Code

Pulled code out from main into separate files.