Index: trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/DefaultIterationDetectionRule.java
===================================================================
--- trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/DefaultIterationDetectionRule.java	(revision 922)
+++ trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/DefaultIterationDetectionRule.java	(revision 970)
@@ -120,5 +120,4 @@
         
         
-        // to find longer iterations first, start with long sequences
         SubSequences subSequences = getEqualSubsequences(parent, treeBuilder, nodeFactory);
 
@@ -173,4 +172,5 @@
         SubSequences subSequences = null;
 
+        // to find longer iterations first, start with long sequences
         FIND_ITERATION:
         for (int end = parent.getChildren().size(); end > 0; end--) {
@@ -496,8 +496,8 @@
     /**
      * <p>
-     * merges equal selections. This is done through trying to merge each node of selections with
-     * each other. For this, the method
-     * {@link #mergeEqualNodes(List, ITaskTreeBuilder, ITaskTreeNodeFactory)} is called with a
-     * join of the child list of both selections.
+     * merges equal selections. This is done by adding those children of the second selection to
+     * the first selection that can not be merged with any of the children of the first selection.
+     * If a merge is possible and this merge is not a simple selection of the merged children,
+     * then the merged child replaces the child of the first selection which was merged.
      * </p>
      *
@@ -507,5 +507,5 @@
      * @param nodeFactory the node factory that can be used for instantiating task tree nodes
      * 
-     * @return the result of the merge which is not null
+     * @return the result of the merge which is never null
      */
     private ITaskTreeNode mergeEqualSelections(ISelection            selection1,
@@ -514,15 +514,38 @@
                                                ITaskTreeNodeFactory  nodeFactory)
     {
-        ISelection mergeResult = nodeFactory.createNewSelection();
-            
-        for (int i = 0; i < selection1.getChildren().size(); i++) {
-            treeBuilder.addChild(mergeResult, selection1.getChildren().get(i));
-        }
-        
+        ISelection mergeResult = selection1;
+        
+        ITaskTreeNode childToMerge = null;
+        ITaskTreeNode mergedChild = null;
+        
+        // check for each child of selection 2 if it is a duplicate of one of the children
+        // if selection 1. If not, add it as further child to the merge result, else skip it.
         for (int i = 0; i < selection2.getChildren().size(); i++) {
-            treeBuilder.addChild(mergeResult, selection2.getChildren().get(i));
-        }
-        
-        mergeEqualNodes(mergeResult.getChildren(), treeBuilder, nodeFactory);
+            childToMerge = selection2.getChildren().get(i);
+            for (int j = 0; j < selection1.getChildren().size(); j++) {
+                mergedChild = mergeEqualTasks
+                    (selection1.getChildren().get(j), childToMerge, treeBuilder, nodeFactory);
+                
+                // a merge must not be a selection, except it is one of the children. Otherwise
+                // no real merge was done.
+                if ((mergedChild != null) &&
+                    ((!(mergedChild instanceof ISelection)) ||
+                     (selection1.getChildren().get(j) == mergedChild) ||
+                     (childToMerge == mergedChild)))
+                {
+                    // we found a real merge. So replace the original child in selection 1 with
+                    // the merged child
+                    treeBuilder.removeChild(selection1, selection1.getChildren().get(j));
+                    treeBuilder.addChild(selection1, mergedChild);
+                    mergedChild = null;
+                    childToMerge = null;
+                    break;
+                }
+            }
+            
+            if (childToMerge != null) {
+                treeBuilder.addChild(selection1, childToMerge);
+            }
+        }
         
         return mergeResult;
