Patches before this are backported from my master-witness work in progress branch...