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 1663)
+++ /branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/SequenceForTaskDetectionRuleAlignment.java	(revision 1664)
@@ -203,17 +203,4 @@
 							.hasNext();) {
 						int start = jt.next();
-						// TODO: Debug Output
-						/*
-						 * System.out.println("Found match ");
-						 * pattern.getFirstSequence().printSequence();
-						 * pattern.getSecondSequence().printSequence();
-						 * System.out.println("in sequence " + (j+1) +
-						 * " at position " + start); for(int
-						 * k=0;k<tempns.getSequence().length;k++) {
-						 * System.out.print(k + ": " + tempns.getSequence()[k] +
-						 * " ");
-						 * System.out.println(appData.getNumber2Task().get(
-						 * tempns.getSequence()[k])); }
-						 */
 						pattern.addOccurence(new MatchOccurence(start, start
 								+ pattern.size(), j));
@@ -255,6 +242,5 @@
 							+ (oc.getStartindex())
 							+ "-"
-							+ (oc.getStartindex() + matchseqs.get(i)
-									.getFirstSequence().size()));
+							+ oc.getEndindex());
 					System.out.println();
 
@@ -265,4 +251,52 @@
 					}
 
+					// Check if nothing has been replaced in the sequence we
+					// want to replace
+					if (replacedOccurences.get(oc.getSequenceId()) == null) {
+						replacedOccurences.put(oc.getSequenceId(),
+								new LinkedList<MatchOccurence>());
+					} else {
+						// check if we have any replaced occurence with indexes
+						// smaller than ours. If so, we need to adjust our start
+						// and endpoints
+						// of the replacement.
+						// Also do a check if we have replaced this specific
+						// MatchOccurence in this sequence already. Jump to the
+						// next occurence if this is the case.
+						// This is no more neccessary once the matches are
+						// harmonized.
+						for (Iterator<MatchOccurence> jt = replacedOccurences
+								.get(oc.getSequenceId()).iterator(); jt
+								.hasNext();) {
+							MatchOccurence tmpOC = jt.next();
+							
+							if (oc.getStartindex() >= tmpOC.getStartindex() && oc.getStartindex()<=tmpOC.getEndindex()) {
+								System.out.println("This position has already been replaced in this iteration");
+								continue invalidOccurence;
+							}
+							if (oc.getEndindex()>=tmpOC.getStartindex()) {
+								continue invalidOccurence;
+								
+							}
+							else if (oc.getStartindex()>tmpOC.getEndindex()) {
+								int diff = tmpOC.getEndindex()
+										- tmpOC.getStartindex();
+								// Just to be sure.
+								if (diff > 0) {
+									System.out.println("Old index: Start: "
+											+ oc.getStartindex() + " End "
+											+ oc.getEndindex());
+									oc.setStartindex(oc.getStartindex() - diff+1);
+									oc.setEndindex(oc.getEndindex() - diff+1);
+									System.out.println("Updated index: Start: "
+											+ oc.getStartindex() + " End "
+											+ oc.getEndindex());
+								} else {
+									Console.traceln(Level.WARNING,
+											"End index of a Match before start. This should never happen");
+								}
+							}
+						}
+					}
 					ISequenceInstance sequenceInstances = RuleUtils
 							.createNewSubSequenceInRange(
@@ -275,8 +309,5 @@
 							+ sequenceInstances.size()
 							- RuleUtils.missedOptionals);
-
-					updateMatchIndexes(matchseqs, oc.getSequenceId(),
-							oc.getStartindex(), oc.getEndindex());
-
+					replacedOccurences.get(oc.getSequenceId()).add(oc);
 				}
 			}
