Improved header writing