Added ability to disable Flatten on certain conditions