simplify code, decrease cyclomatic complexity from 13 to 11