Flattening logic