Move write of header to separate function.