Refactored upgrade script. Added permission check.