Backport Paul's fixes from Emacs trunk (2/2)