fix some bug while reading and writing