Should this be reversed?  So that if a failure occurs when writing the content, there is some detail about it in the header that is written first instead of second?