fix issues with flatten method