Tree search passes tests, needs refactoring