fixed bug in flatten