Fixed tests and refactored upgrade script.