fixing bugs in read/write header