Improved upgrade script