Bug fix in flattening