fix flatten method