It all works on the example inputs!  I need to write the output to a file apparently, but whatever it all works.  It might not work on Emily's test inputs, but I'll deal with that tomorrow.