Refactoring to reduce code complexity