Move writeHeader() to a separate method.