write headers after error checking