fix bugs in writer.py