Refactored by improving structure and readability