updated flatten fixing order of elements