Refactored code to be cleaner and more easily extended