Several tweaks to fix upgrade bugs.