Refactored code and made the output prettier.