fix flatter logic: manipulate temp file first instead of original