internal static void AddGameObjectsToPrefabAndConnect(GameObject[] gameObjects, Object targetPrefab)
        {
            if (gameObjects == null)
            {
                throw new ArgumentNullException("gameObjects");
            }

            if (gameObjects.Length == 0)
            {
                throw new ArgumentException("gameObjects array is empty");
            }

            if (targetPrefab == null)
            {
                throw new ArgumentNullException("targetPrefab");
            }

            if (!PrefabUtility.IsPartOfPrefabAsset(targetPrefab))
            {
                throw new ArgumentException("Target Prefab has to be a Prefab Asset");
            }

            Object targetPrefabInstance = null;

            var targetPrefabObject = PrefabUtility.GetPrefabAssetHandle(targetPrefab);

            foreach (GameObject go in gameObjects)
            {
                if (go == null)
                {
                    throw new ArgumentException("GameObject in input 'gameObjects' array is null");
                }

                if (EditorUtility.IsPersistent(go))  // Prefab asset
                {
                    throw new ArgumentException("Game object is part of a prefab");
                }

                var parentPrefabInstance = GetParentPrefabInstance(go);
                if (parentPrefabInstance == null)
                {
                    throw new ArgumentException("GameObject is not (directly) parented under a target Prefab instance.");
                }

                if (targetPrefabInstance == null)
                {
                    targetPrefabInstance = parentPrefabInstance;
                    if (!IsPrefabInstanceObjectOf(go.transform.parent, targetPrefabObject))
                    {
                        throw new ArgumentException("GameObject is not parented under a target Prefab instance.");
                    }
                }
                else
                {
                    if (parentPrefabInstance != targetPrefabInstance)
                    {
                        throw new ArgumentException("GameObjects must be parented under the same Prefab instance.");
                    }
                }

                if (PrefabUtility.IsPartOfNonAssetPrefabInstance(go))
                {
                    var correspondingGO             = PrefabUtility.GetCorrespondingObjectFromSource(go);
                    var correspondingGOPrefabObject = PrefabUtility.GetPrefabAssetHandle(correspondingGO);
                    if (targetPrefabObject == correspondingGOPrefabObject)
                    {
                        throw new ArgumentException("GameObject is already part of target prefab");
                    }
                }
            }

            string prefabGUID = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(targetPrefab));

            if (!VerifyNestingFromScript(gameObjects, prefabGUID, null))
            {
                throw new ArgumentException("Cyclic nesting detected");
            }

            AddGameObjectsToPrefabAndConnect_Internal(gameObjects, targetPrefab);
        }