This likely should be an error message, but this is just a first pass at the algorithm.  Note that there are no tests exercising any of this code yet (no extends, no implements).  This gets the job done for now, but will obviously be revisited when we figure out whether it works at all.  :-)