Backported fixes from TESTING branch.