Fix bugs and add upgrade necessity check