Fix the bug in flattening