Refactored more of the code