Refactored code to be more concise and understandable