Merge key trace header correctness fixes from the rewrite branch into master