Suggested refactoring to reduce duplication and improve readability