Index: /branches/autoquest-core-tasktrees-alignment-test/src/test/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/SequenceForTaskDetectionRuleAlignmentTest.java
===================================================================
--- /branches/autoquest-core-tasktrees-alignment-test/src/test/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/SequenceForTaskDetectionRuleAlignmentTest.java	(revision 1692)
+++ /branches/autoquest-core-tasktrees-alignment-test/src/test/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/SequenceForTaskDetectionRuleAlignmentTest.java	(revision 1693)
@@ -17,4 +17,7 @@
 import org.junit.Test;
 
+import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.Match;
+import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.NumberSequence;
+
 /**
  * @author Patrick Harms
@@ -22,4 +25,20 @@
 public class SequenceForTaskDetectionRuleAlignmentTest extends AbstractTemporalRelationshipTC {
 
+	
+	@Test
+	public void test_MatchAsSequences() {
+		Match m1 = new Match();
+		int[] pat1 = new int[]{2,1,3,-1,3,5,4,8,3,1};
+		int[] pat2 = new int[]{1,2,3, 4,3,4,5,3,3,-1};
+		NumberSequence ns1 = new NumberSequence(10);
+		NumberSequence ns2 = new NumberSequence(10);
+		ns1.setSequence(pat1);
+		ns2.setSequence(pat2);
+		m1.setFirstSequence(ns1);
+		m1.setSecondSequence(ns1);
+		
+		
+	}
+	
     /**
      *
Index: /branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/algorithms/Match.java
===================================================================
--- /branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/algorithms/Match.java	(revision 1692)
+++ /branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/algorithms/Match.java	(revision 1693)
@@ -11,5 +11,5 @@
 	private LinkedList<MatchOccurence> occurences;
 
-	Match() {
+	public Match() {
 		matchseqs = new ArrayList<NumberSequence>(2);
 		occurences = new LinkedList<MatchOccurence>();
Index: /branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/algorithms/NumberSequence.java
===================================================================
--- /branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/algorithms/NumberSequence.java	(revision 1692)
+++ /branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/algorithms/NumberSequence.java	(revision 1693)
@@ -45,5 +45,14 @@
 
 	//Recursive check if sequence contains pattern at position i
-	private boolean matches(int i, int[] p1, int[] p2 ,int ip1,int ip2,boolean jumped1,boolean jumped2) {
+	private boolean matches(int i, 
+				int[] p1, 
+				int[] p2 ,
+				int ip1,
+				int ip2,
+				boolean jumped1, //True if there was a gap in Sequence 1 of the pattern
+				boolean jumped2, //True if there was a gap in Sequence 2 of the pattern
+				boolean hadSelection, //True if the last match was a selection 
+				boolean matchseq1,
+				boolean matchseq2) {
 		
 		if(p1.length==ip1) {
@@ -56,18 +65,38 @@
 			return false;
 		}
-		if((p1[ip1]==sequence[i]||p2[ip2]==sequence[i]) && jumped1) {
-			return matches(i+1,p1,p2,ip1+1,ip2+2,false,false);
+
+		boolean foundselection=(!(p1[ip1] == p2[ip2])&&!(p1[ip1]==-1||p2[ip2]==-1));
+		boolean matchInFirstPattern = (p1[ip1]==sequence[i]);
+		boolean matchInSecondPattern = (p2[ip2]==sequence[i]);
+		
+		if(foundselection && hadSelection) {
+			if((matchInFirstPattern && matchseq1) || (matchInSecondPattern && matchseq2)){
+				if(jumped1) {
+					return matches(i+1,p1,p2,ip1+1,ip2+2,false,false,foundselection,matchInFirstPattern,matchInSecondPattern);
+				}
+				if(jumped2) {
+					return matches(i+1,p1,p2,ip1+2,ip2+1,false,false,foundselection,matchInFirstPattern,matchInSecondPattern);
+				}
+				return matches(i+1,p1,p2,ip1+1,ip2+1,false,false,foundselection,matchInFirstPattern,matchInSecondPattern);
+			}	
+			else {
+				return false;
+			}
 		}
-		if((p1[ip1]==sequence[i]||p2[ip2]==sequence[i]) && jumped2) {
-			return matches(i+1,p1,p2,ip1+2,ip2+1,false,false);
+
+		if((matchInFirstPattern||matchInSecondPattern) && jumped1) {
+			return matches(i+1,p1,p2,ip1+1,ip2+2,false,false,foundselection,matchInFirstPattern,matchInSecondPattern);
 		}
-		if(p1[ip1]==sequence[i]||p2[ip2]==sequence[i]) {
-			return matches(i+1,p1,p2,ip1+1,ip2+1,false,false);
+		if((matchInFirstPattern||matchInSecondPattern) && jumped2) {
+			return matches(i+1,p1,p2,ip1+2,ip2+1,false,false,foundselection,matchInFirstPattern,matchInSecondPattern);
+		}
+		if(matchInFirstPattern||matchInSecondPattern) {
+			return matches(i+1,p1,p2,ip1+1,ip2+1,false,false,foundselection,matchInFirstPattern,matchInSecondPattern);
 		}
 		if(p1[ip1]==-1) {
-			return matches(i,p1,p2,ip1+1,ip2,true,false);
+			return matches(i,p1,p2,ip1+1,ip2,true,false,false,false,false);
 		}
 		if(p2[ip2]==-1) {
-			return matches(i,p1,p2,ip1,ip2+1,false,true);
+			return matches(i,p1,p2,ip1,ip2+1,false,true,false,false,false);
 		}
 	
@@ -83,5 +112,5 @@
 
 		while (i < sequence.length ) {
-			if(matches(i,pat1,pat2,0,0,false,false)) {
+			if(matches(i,pat1,pat2,0,0,false,false,false,false,false)) {
 				result.add(i);
 			}
Index: /branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/matrix/ObjectDistanceSubstitionMatrix.java
===================================================================
--- /branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/matrix/ObjectDistanceSubstitionMatrix.java	(revision 1692)
+++ /branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/matrix/ObjectDistanceSubstitionMatrix.java	(revision 1693)
@@ -105,5 +105,4 @@
 	}
 	
-	//TODO: Calculate distance here
 	private float distanceBetweenTasks(ITask task1, ITask task2) {
 		return 0;
Index: /branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/RuleUtils.java
===================================================================
--- /branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/RuleUtils.java	(revision 1692)
+++ /branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/RuleUtils.java	(revision 1693)
@@ -24,4 +24,5 @@
 import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
 import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance;
+import de.ugoe.cs.autoquest.tasktrees.treeifc.IStructuringTemporalRelationship;
 import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
 import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskBuilder;
@@ -102,15 +103,41 @@
 	 * 
 	 * @return the replacement for the range
+	 * @throws  
 	 */
 	static ISequenceInstance createNewSubSequenceInRange(
 			ITaskInstanceList parent, int startIndex, int endIndex,
-			ISequence model, ITaskFactory taskFactory, ITaskBuilder taskBuilder) {
+			ISequence model, ITaskFactory taskFactory, ITaskBuilder taskBuilder)   {
 		ISequenceInstance subsequence = taskFactory
 				.createNewTaskInstance(model);
 		
-		//for (int i=0; i<subsequence.getSequence().getChildren().size();i++) {
-		//	System.out.println(subsequence.getSequence().getChildren().get(i));	
-		//}
-		//System.out.println();
+		// TODO: Debug output
+				System.out.println("PRINTING MODEL: ");
+				for (int i = 0; i < subsequence.getSequence().getChildren().size(); i++) {
+					System.out.println(subsequence.getSequence().getChildren().get(i));
+
+					if (subsequence.getSequence().getChildren().get(i).getType() == "selection") {
+						for (int j = 0; j < ((ISelection) subsequence.getSequence().getChildren().get(i))
+								.getChildren().size(); j++) {
+							if(((IStructuringTemporalRelationship) subsequence.getSequence().getChildren().get(i)).getChildren().get(j).getType() =="sequence")
+							{
+								ISequence foo =  (ISequence) ((ISelection) (subsequence.getSequence().getChildren().get(i))).getChildren().get(j);
+								System.out.println("\t" + foo);
+								for(int k=0; k< foo.getChildren().size();k++) {
+									System.out.println("\t\t" +foo.getChildren().get(k));
+								}
+								System.out.println();
+							}
+							else{
+								System.out.println("\t"
+										+ ((ISelection) subsequence.getSequence().getChildren().get(i))
+												.getChildren().get(j));
+							}
+						
+						}
+					}
+				
+				}
+				System.out.println();
+		
 		//TODO: This is dirty!
 		missedOptionals=0;
@@ -127,5 +154,5 @@
 						
 					if(((IMarkingTemporalRelationship) tempTask).getMarkedTask() == parent.get(startIndex).getTask()) {
-						//System.out.println("Adding OptionalInstance " + parent.get(startIndex) + " to " + tempTask.getType());
+						System.out.println("Adding OptionalInstance " + parent.get(startIndex) + " to " + tempTask.getType());
 						IOptionalInstance optional = taskFactory.createNewTaskInstance((IOptional) tempTask);
 						taskBuilder.setChild(optional, parent.get(startIndex));
@@ -141,22 +168,49 @@
 					}				
 			} else if (tempTask.getType() == "selection") {
-				//System.out.println("Adding SelectionInstance " + parent.get(startIndex) + " to " + tempTask.getType());
+				
 				ISelectionInstance selection = taskFactory.createNewTaskInstance((ISelection) tempTask);
-				taskBuilder.setChild(selection, parent.get(startIndex) );
+				ISelection tmpSel = (ISelection)tempTask;
+				if(tmpSel.getChildren().get(0).getType() == "sequence" && tmpSel.getChildren().get(1).getType()=="sequence") {
+					System.out.println("Adding SequenceInstance " + parent.get(startIndex) + " to " + tempTask);
+					ISequenceInstance selseq = null;
+					System.out.println("IDS: " + parent.get(startIndex).getTask().getId() + " " +((ISequence)tmpSel.getChildren().get(0)).getChildren().get(0).getId());
+					System.out.println("IDS: " + parent.get(startIndex).getTask().getId() + " " +((ISequence)tmpSel.getChildren().get(1)).getChildren().get(0).getId());
+					if(parent.get(startIndex).getTask().getId() == ((ISequence)tmpSel.getChildren().get(0)).getChildren().get(0).getId()) {
+						selseq = taskFactory.createNewTaskInstance((ISequence) tmpSel.getChildren().get(0));
+					}
+					else if(parent.get(startIndex).getTask().getId() == ((ISequence)tmpSel.getChildren().get(1)).getChildren().get(0).getId()) {
+						selseq = taskFactory.createNewTaskInstance((ISequence) tmpSel.getChildren().get(1));
+					}
+					else {
+						//throw new Exception("Error while creating subsequence of a selection");
+						
+						System.out.println("Error while creating subsequence of a selection");
+					}
+					
+					for (int k=0;k<selseq.getSequence().getChildren().size();k++) {
+						taskBuilder.addChild(selseq,parent.get(startIndex));
+						taskBuilder.removeTaskInstance(parent, startIndex);
+						i++;
+					}
+					taskBuilder.setChild(selection, selseq);
+				}
+				else
+				{
+					System.out.println("Adding SelectionInstance " + parent.get(startIndex) + " to " + tempTask);
+					taskBuilder.setChild(selection, parent.get(startIndex));
+				}
 				taskBuilder.addChild(subsequence,selection);
 
 			} else if (tempTask.getType() == "sequence") {
-				//ISequenceInstance sequence = taskFactory.createNewTaskInstance((ISequence) tempTask);
-				//taskBuilder.addChild(sequence, parent.get(i));
-				//taskBuilder.addChild(subsequence,sequence);
+				System.out.println("Adding SequenceInstance " + parent.get(startIndex) + " to " + tempTask);
+				ISequenceInstance sequence = taskFactory.createNewTaskInstance((ISequence) tempTask);
+				taskBuilder.addChild(sequence, parent.get(i));
+				taskBuilder.addChild(subsequence,sequence);
+				//taskBuilder.addChild(subsequence, parent.get(startIndex));
+			} else if (tempTask.getType() == "iteration") {
+				System.out.println("Adding IterationInstance " + parent.get(startIndex) + " to " + tempTask);
 				taskBuilder.addChild(subsequence, parent.get(startIndex));
-			} else if (tempTask.getType() == "iteration") {
-				//IIterationInstance iteration = taskFactory.createNewTaskInstance((IIteration) tempTask);
-				//taskBuilder.addChild(iteration, parent.get(startIndex));
-				//taskBuilder.addChild(subsequence, iteration);
-				taskBuilder.addChild(subsequence, parent.get(startIndex));
-				//System.out.println("Adding IterationInstance " + parent.get(startIndex) + " to " + tempTask.getType());
 			} else {
-				//System.out.println("Adding EventInstance " + parent.get(startIndex) + " to " + tempTask.getType());
+				System.out.println("Adding EventInstance " + parent.get(startIndex) + " to " + tempTask);
 				taskBuilder.addChild(subsequence, parent.get(startIndex));
 			}
Index: /branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/SequenceForTaskDetectionRuleAlignment.java
===================================================================
--- /branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/SequenceForTaskDetectionRuleAlignment.java	(revision 1692)
+++ /branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/SequenceForTaskDetectionRuleAlignment.java	(revision 1693)
@@ -68,5 +68,4 @@
 class SequenceForTaskDetectionRuleAlignment implements ISessionScopeRule {
 
-	
 	private int iteration = 0;
 	/**
@@ -92,5 +91,4 @@
 	 */
 	private TaskHandlingStrategy preparationTaskHandlingStrategy;
-
 
 	/**
@@ -139,17 +137,17 @@
 	public RuleApplicationResult apply(List<IUserSession> sessions) {
 		RuleApplicationData appData = new RuleApplicationData(sessions);
-		
+
 		harmonizeEventTaskInstancesModel(appData);
 		do {
 			iteration++;
-			
+
 			appData.detectedAndReplacedTasks = false;
-			appData.getStopWatch().start("whole loop"); 
+			appData.getStopWatch().start("whole loop");
 			detectAndReplaceIterations(appData);
-			appData.getStopWatch().start("task replacement"); 
+			appData.getStopWatch().start("task replacement");
 			detectAndReplaceTasks(appData); //
-			appData.getStopWatch().stop("task replacement"); 
+			appData.getStopWatch().stop("task replacement");
 			appData.getStopWatch().stop("whole loop");
-			appData.getStopWatch().dumpStatistics(System.out); 
+			appData.getStopWatch().dumpStatistics(System.out);
 			appData.getStopWatch().reset();
 
@@ -181,5 +179,4 @@
 				templist.getSequence()[j] = taskInstance.getTask().getId();
 			}
-			//System.out.println();
 			// Each NumberSequence is identified by its id, beginning to count
 			// at zero
@@ -207,5 +204,6 @@
 				"harmonizing task model of event task instances");
 		appData.getStopWatch().start("harmonizing event tasks");
-		SymbolMap<ITaskInstance, ITask> uniqueTasks = preparationTaskHandlingStrategy.createSymbolMap();
+		SymbolMap<ITaskInstance, ITask> uniqueTasks = preparationTaskHandlingStrategy
+				.createSymbolMap();
 
 		TaskInstanceComparator comparator = preparationTaskHandlingStrategy
@@ -223,8 +221,9 @@
 
 				if (task == null) {
-					uniqueTasks.addSymbol(taskInstance,
+					uniqueTasks.addSymbol(taskInstance, taskInstance.getTask());
+					appData.getUniqueTasks().add(taskInstance.getTask());
+					appData.getNumber2Task().put(
+							taskInstance.getTask().getId(),
 							taskInstance.getTask());
-							appData.getUniqueTasks().add(taskInstance.getTask());
-							appData.getNumber2Task().put(taskInstance.getTask().getId(),taskInstance.getTask());
 				} else {
 					taskBuilder.setTask(taskInstance, task);
@@ -323,5 +322,5 @@
 
 			IIteration iteration = taskFactory.createNewIteration();
-			appData.getUniqueTasks().add( iteration);
+			appData.getUniqueTasks().add(iteration);
 			appData.getNumber2Task().put(iteration.getId(), iteration);
 			iterations.put(iteratedTask, iteration);
@@ -350,5 +349,8 @@
 								.createNewTaskInstance(iteration);
 						iterationInstances.get(iteration)
-								.add(iterationInstance);//TODO:: Don't create TaskInstances here, use a set of tasks instead
+								.add(iterationInstance);// TODO:: Don't create
+														// TaskInstances here,
+														// use a set of tasks
+														// instead
 						taskBuilder.addTaskInstance(session, index,
 								iterationInstance);
@@ -366,78 +368,87 @@
 			}
 		}
-		  
-        for (Map.Entry<IIteration, List<IIterationInstance>> entry : iterationInstances.entrySet())
-        {
-            harmonizeIterationInstancesModel(entry.getKey(), entry.getValue());
-        }
-	}
-
-	
-	/**
-     * <p>
-     * TODO clarify why this is done
-     * </p>
-     */
-    private void harmonizeIterationInstancesModel(IIteration               iteration,
-                                                  List<IIterationInstance> iterationInstances)
-    {
-        List<ITask> iteratedTaskVariants = new LinkedList<ITask>();
-        TaskInstanceComparator comparator = preparationTaskHandlingStrategy.getTaskComparator();
-        
-        // merge the lexically different variants of iterated task to a unique list 
-        for (IIterationInstance iterationInstance : iterationInstances) {
-            for (ITaskInstance executionVariant : iterationInstance) {
-                ITask candidate = executionVariant.getTask();
-            
-                boolean found = false;
-                for (ITask taskVariant : iteratedTaskVariants) {
-                    if (comparator.areLexicallyEqual(taskVariant, candidate)) {
-                        taskBuilder.setTask(executionVariant, taskVariant);
-                        found = true;
-                        break;
-                    }
-                }
-                
-                if (!found) {
-                    iteratedTaskVariants.add(candidate);
-                }
-            }
-        }
-        
-        // if there are more than one lexically different variant of iterated tasks, adapt the
-        // iteration model to be a selection of different variants. In this case also adapt
-        // the generated iteration instances to correctly contain selection instances. If there
-        // is only one variant of an iterated task, simply set this as the marked task of the
-        // iteration. In this case, the instances can be preserved as is
-        if (iteratedTaskVariants.size() > 1) {
-            ISelection selection = taskFactory.createNewSelection();
-            
-            for (ITask variant : iteratedTaskVariants) {
-                taskBuilder.addChild(selection, variant);
-            }
-            
-            taskBuilder.setMarkedTask(iteration, selection);
-            
-            for (IIterationInstance instance : iterationInstances) {
-                for (int i = 0; i < instance.size(); i++) {
-                    ISelectionInstance selectionInstance =
-                        taskFactory.createNewTaskInstance(selection);
-                    taskBuilder.setChild(selectionInstance, instance.get(i));
-                    taskBuilder.setTaskInstance(instance, i, selectionInstance);
-                }
-            }
-        }
-        else {
-            taskBuilder.setMarkedTask(iteration, iteratedTaskVariants.get(0));
-        }
-    }
-
-
-	ISequence matchAsSequence(RuleApplicationData appData, Match m) {
-
+
+		for (Map.Entry<IIteration, List<IIterationInstance>> entry : iterationInstances
+				.entrySet()) {
+			harmonizeIterationInstancesModel(entry.getKey(), entry.getValue());
+		}
+	}
+
+	/**
+	 * <p>
+	 * TODO clarify why this is done
+	 * </p>
+	 */
+	private void harmonizeIterationInstancesModel(IIteration iteration,
+			List<IIterationInstance> iterationInstances) {
+		List<ITask> iteratedTaskVariants = new LinkedList<ITask>();
+		TaskInstanceComparator comparator = preparationTaskHandlingStrategy
+				.getTaskComparator();
+
+		// merge the lexically different variants of iterated task to a unique
+		// list
+		for (IIterationInstance iterationInstance : iterationInstances) {
+			for (ITaskInstance executionVariant : iterationInstance) {
+				ITask candidate = executionVariant.getTask();
+
+				boolean found = false;
+				for (ITask taskVariant : iteratedTaskVariants) {
+					if (comparator.areLexicallyEqual(taskVariant, candidate)) {
+						taskBuilder.setTask(executionVariant, taskVariant);
+						found = true;
+						break;
+					}
+				}
+
+				if (!found) {
+					iteratedTaskVariants.add(candidate);
+				}
+			}
+		}
+
+		// if there are more than one lexically different variant of iterated
+		// tasks, adapt the
+		// iteration model to be a selection of different variants. In this case
+		// also adapt
+		// the generated iteration instances to correctly contain selection
+		// instances. If there
+		// is only one variant of an iterated task, simply set this as the
+		// marked task of the
+		// iteration. In this case, the instances can be preserved as is
+		if (iteratedTaskVariants.size() > 1) {
+			ISelection selection = taskFactory.createNewSelection();
+
+			for (ITask variant : iteratedTaskVariants) {
+				taskBuilder.addChild(selection, variant);
+			}
+
+			taskBuilder.setMarkedTask(iteration, selection);
+
+			for (IIterationInstance instance : iterationInstances) {
+				for (int i = 0; i < instance.size(); i++) {
+					ISelectionInstance selectionInstance = taskFactory
+							.createNewTaskInstance(selection);
+					taskBuilder.setChild(selectionInstance, instance.get(i));
+					taskBuilder.setTaskInstance(instance, i, selectionInstance);
+				}
+			}
+		} else {
+			taskBuilder.setMarkedTask(iteration, iteratedTaskVariants.get(0));
+		}
+	}
+
+	/**
+	 * @param appData
+	 * @param m
+	 * @return
+	 */
+	public ISequence matchAsSequence(RuleApplicationData appData, Match m) {
+		
+		System.out.println("DEBUGGING MODEL GENERATION");
+		System.out.println();
+		
 		ISequence sequence = taskFactory.createNewSequence();
 		appData.uniqueTasks.add(sequence);
 		appData.number2task.put(sequence.getId(), sequence);
-		
 
 		int[] first = m.getFirstSequence().getSequence();
@@ -450,5 +461,5 @@
 			// far, just to handle it
 			if (first[i] == -1 && second[i] == -1) {
-				// TODO: Do nothing?
+				// Do nothing here.
 			}
 			// Both events are equal, we can simply add the task referring to
@@ -478,31 +489,76 @@
 				taskBuilder.addChild(sequence, optional);
 			}
-			// Both tasks are not equal, we need to insert a selection here
+			// Both tasks are not equal, we need to insert a selection here.
+			// Check if the next position is not a selection
+			else if (i < first.length - 1) {
+
+				if ((first[i] != second[i])
+						&& ((first[i + 1] == second[i + 1]
+								|| first[i + 1] == -1 || second[i + 1] == -1))) {
+
+					ISelection selection = taskFactory.createNewSelection();
+					appData.getUniqueTasks().add(selection);
+					appData.number2task.put(selection.getId(), selection);
+					taskBuilder.addChild(selection, appData.getNumber2Task()
+							.get(first[i]));
+					taskBuilder.addChild(selection, appData.getNumber2Task()
+							.get(second[i]));
+					taskBuilder.addChild(sequence, selection);
+				} else {
+					System.out.println("SELECTION OF SEQUENCES FOUND!");
+					boolean selectionfound = true;
+					ISelection selection = taskFactory.createNewSelection();
+					appData.getUniqueTasks().add(selection);
+					appData.number2task.put(selection.getId(), selection);
+
+					ISequence subsequence1 = taskFactory.createNewSequence();
+					appData.uniqueTasks.add(subsequence1);
+					appData.number2task.put(subsequence1.getId(), subsequence1);
+
+					ISequence subsequence2 = taskFactory.createNewSequence();
+					appData.uniqueTasks.add(subsequence2);
+					appData.number2task.put(subsequence2.getId(), subsequence2);
+
+					taskBuilder.addChild(selection, subsequence1);
+					taskBuilder.addChild(selection, subsequence2);
+					taskBuilder.addChild(sequence,selection);
+					while (i < first.length - 1 && selectionfound) {
+						selectionfound = false;
+						taskBuilder.addChild(subsequence1, appData
+								.getNumber2Task().get(first[i]));
+						taskBuilder.addChild(subsequence2, appData
+								.getNumber2Task().get(second[i]));
+						if (first[i + 1] != second[i + 1] && first[i + 1] != -1
+								&& second[i + 1] != -1) {
+							selectionfound = true;
+						}
+						i++;
+					}
+					if(i==first.length-1 && selectionfound) {
+						taskBuilder.addChild(subsequence1, appData
+								.getNumber2Task().get(first[i]));
+						taskBuilder.addChild(subsequence2, appData
+								.getNumber2Task().get(second[i]));
+					}
+				}
+			}
+
 			else {
-				ISelection selection = taskFactory.createNewSelection();
-				appData.getUniqueTasks().add(selection);
-				appData.number2task.put(selection.getId(), selection);
-				taskBuilder.addChild(selection,
-						appData.getNumber2Task().get(first[i]));
-				taskBuilder.addChild(selection,
-						appData.getNumber2Task().get(second[i]));
-				taskBuilder.addChild(sequence, selection);
-			}
-		}
-
-		// TODO: Debug output
-		/*
-		 * for (int i =0;i<sequence.getChildren().size();i++) {
-		 * System.out.println(sequence.getChildren().get(i));
-		 * 
-		 * if(sequence.getChildren().get(i).getType() == "selection") { for(int
-		 * j=0; j< ((ISelection)
-		 * sequence.getChildren().get(i)).getChildren().size();j++) {
-		 * System.out.println("\t" +((ISelection)
-		 * sequence.getChildren().get(i)).getChildren().get(j)); } } }
-		 */
+				if ((first[i] != second[i])) {
+
+					ISelection selection = taskFactory.createNewSelection();
+					appData.getUniqueTasks().add(selection);
+					appData.number2task.put(selection.getId(), selection);
+					taskBuilder.addChild(selection, appData.getNumber2Task()
+							.get(first[i]));
+					taskBuilder.addChild(selection, appData.getNumber2Task()
+							.get(second[i]));
+					taskBuilder.addChild(sequence, selection);
+				}
+			}
+
+		}
 		return sequence;
 	}
-
 
 	/**
@@ -546,5 +602,5 @@
 			}
 		}
-		
+
 		Console.traceln(Level.FINEST,
 				"retrieving significant sequence pieces:  100%");
@@ -598,5 +654,5 @@
 			// well
 			if (matchseqs.get(i).occurenceCount() > 2) {
-				
+
 				appData.detectedAndReplacedTasks = true;
 				ISequence task = matchAsSequence(appData, matchseqs.get(i));
@@ -604,24 +660,24 @@
 						.get(i).getOccurences().iterator(); it.hasNext();) {
 					MatchOccurence oc = it.next();
-
-					if(iteration==-1) {
-					  System.out.println("Trying to replace sequence: ");
-					  matchseqs.get(i).getFirstSequence().printSequence();
-					  matchseqs.get(i).getSecondSequence().printSequence();
-					  System.out.println(" in session number: " +
-					  (oc.getSequenceId() + 1) + " at position " +
-					  (oc.getStartindex()) + "-" + oc.getEndindex());
-					  System.out.println();
+					
+					if (iteration > 0) {
 					 
-
-					  System.out.println("Printing session: ");
-					 for (int j = 0; j <
-					appData.getSessions().get(oc.getSequenceId()).size();
-					 j++) {
-					 System.out.println(j + ": "
-					 + appData.getSessions().get(oc.getSequenceId()).get(j));
-					 }
+					System.out.println("Trying to replace sequence: ");
+					matchseqs.get(i).getFirstSequence().printSequence();
+					matchseqs.get(i).getSecondSequence().printSequence();
+					System.out.println(" in session number: "
+							+ (oc.getSequenceId() + 1) + " at position "
+							+ (oc.getStartindex()) + "-" + oc.getEndindex());
+					System.out.println();
+
+					System.out.println("Printing session: ");
+					for (int j = 0; j < appData.getSessions()
+							.get(oc.getSequenceId()).size(); j++) {
+						System.out.println(j
+								+ ": "
+								+ appData.getSessions().get(oc.getSequenceId())
+										.get(j));
 					}
-
+					}
 					// Check if nothing has been replaced in the sequence we
 					// want to replace
@@ -687,6 +743,4 @@
 	}
 
-	
-
 	/**
      * 
@@ -696,5 +750,5 @@
 		private HashMap<Integer, ITask> number2task;
 
-		//TODO: We Actually just need number2task here
+		// TODO: We Actually just need number2task here
 		private HashSet<ITask> uniqueTasks;
 
