Fixed some bugs in upgrade