This is a reimplemented fc776c20 with the proper fix for the regression introduced by that commit which required a revert.