Refactored code into more logical pieces.