Further fixes upgrade.php

Fixed upgrade.php to address bugs in upgrade process.