Refactored code and improved style