Rough cut at algorithm.
Working on it still.
Debugging tree.
Fixed things.
Seems to be working somewhat.