Refactored some of the code