Moved header writing code into separate function.