Backport critical fixes from trunk