updted flatten