implement flatten