Keep the heirarchy around until the table_id changes, then write it out.
Improve the hierarchy and deal with wide tables better.