Refactored code into more isolated pieces for improved readability.