Most of this was generated.  The code I wrote starts here.