improve write_header