Complete refactoring

Greatly decreased cyclomatic complexity/cleaned code