Factor out DAG.flatten
Add Tup builder

Fixes issue #1.