cleanup and reduce cyclomatic complexity