Refactored code slightly to improve readability