Factored out some logic into separate static methods to allow easier creation of unit tests to test this logic.