Some refactoring, started moving if into tree structure.