fix write bugs