Refactoring of the GetOrAdd method.