Implemented new flattening mechanism