Refactored code and made it prettier