Flattening with constraints

Still experimenting. This is research.