+1 the patch looks good to me, I already implemented half this patch in D14503 which I abandoned in favor of this one.
Thanks a lot @ckertesz!