refactoring for reduce cyclomatic complexity