Fix a bug in writing