A bit more work on the algorithm. Changed from Tuples to a generic triple class for the row headers.