MODE-2043 Added some tests for the upgrade mechanism and a small code refactoring.