Refactoring function to improve readability