Finished refactor

Now supports non-binary trees as inputs. Overall, a much tighter
implementation. Hopefully somewhat faster as well. Passed first unit
test, getting ready to test more complex cases.