Simplify code to avoid too high cyclomatic complexity