Refactored some code into modules for easy reading