5.1.2.final patch

Backport of HV-912 and a whole bunch of other things which we can throw in for good measure.