Refactored code

General refactoring and the introduction of a state system to allow for future command-line options.