Refactored code into nicer layout