Done-ish. All major components exist and seem to work as expected. Needs a better heuristic and more testing.