Fix and simplify upgrade.py