Refactored function for better readability