Backport relevant fixes to CPMT
This backports the relevant fixes from 14f80f647accccfbbc6c4845b1d2dd6107228f8a
By Ragnar Brynjúlfsson