corrected flattening order