fix incorrect flattening order