Refactoring to reduce complexity