changed header write order (thanks strandmon)