refactoring to reduce complexity