Index: branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/algorithms/SmithWatermanRepeated.java
===================================================================
--- branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/algorithms/SmithWatermanRepeated.java	(revision 1584)
+++ branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/algorithms/SmithWatermanRepeated.java	(revision 1585)
@@ -2,5 +2,4 @@
 
 import java.util.ArrayList;
-import java.util.List;
 import java.util.logging.Level;
 
@@ -33,5 +32,5 @@
 
 
-	private List<NumberSequence> alignment;
+	private ArrayList<NumberSequence> alignment;
 	
 	private float scoreThreshold;
@@ -329,9 +328,9 @@
 
 
-	public List<NumberSequence> getAlignment() {
+	public ArrayList<NumberSequence> getAlignment() {
 		return alignment;
 	}
 
-	public void setAlignment(List<NumberSequence> alignment) {
+	public void setAlignment(ArrayList<NumberSequence> alignment) {
 		this.alignment = alignment;
 	}
Index: branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/matrix/BinaryAlignmentStorage.java
===================================================================
--- branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/matrix/BinaryAlignmentStorage.java	(revision 1584)
+++ branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/matrix/BinaryAlignmentStorage.java	(revision 1585)
@@ -9,7 +9,7 @@
     UPGMAMatrix sequenceDistances;
    
-    public BinaryAlignmentStorage(int size) {
-    	alignments = new SmithWatermanRepeated[size][size];
-    	sequenceDistances = new UPGMAMatrix(size);
+    public BinaryAlignmentStorage(int sizex, int sizey) {
+    	alignments = new SmithWatermanRepeated[sizex+1][sizey+1];
+    	sequenceDistances = new UPGMAMatrix(Math.max(sizex,sizey));
     	sequenceDistances.initialize(Double.POSITIVE_INFINITY);
     }
Index: branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/matrix/ObjectDistanceSubstitionMatrix.java
===================================================================
--- branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/matrix/ObjectDistanceSubstitionMatrix.java	(revision 1584)
+++ branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/matrix/ObjectDistanceSubstitionMatrix.java	(revision 1585)
@@ -29,5 +29,5 @@
 		idmapping = new HashMap<Integer, Integer>();
 		matrix = new TriangleMatrix(uniqueTasks.size()+1);
-		gapPenalty = -5;
+		gapPenalty = -3;
 		
 	}
Index: branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/pal/tree/FengDoolittleNode.java
===================================================================
--- branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/pal/tree/FengDoolittleNode.java	(revision 1584)
+++ branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/pal/tree/FengDoolittleNode.java	(revision 1585)
@@ -344,5 +344,5 @@
 	 * @param n2 number of second child
 	 */
-	public void joinChildren( int n1, int n2) {
+	public Node joinChildren( int n1, int n2) {
 
 		if (n1 == n2) {
@@ -376,8 +376,5 @@
 		//System.out.println("Merging " + child1.getIdentifier() + " with " + child2.getIdentifier());
 		
-		if(newNode instanceof FengDoolittleNode) {
-			newNode.setSequences(((FengDoolittleNode) newNode).alignSequences());
-		}
-		
+		return newNode;
 	}
 	
@@ -397,42 +394,9 @@
 
 
-	private ArrayList<NumberSequence> alignSequences() {
-		ArrayList<NumberSequence> alignment = new ArrayList<NumberSequence>();
-		if(this.getChildCount()<3) {
-			
-			Node node1 = getChild(0);
-			Node node2 = getChild(1);
-			
-			int seqCount1 = node1.getSequences().size();
-			int seqCount2 = node2.getSequences().size();
-			
-			//Align 2 sequences
-			if(seqCount1 == 1 && seqCount2 == 1) {
-			}
-			//Align a sequence to a group
-			else if( seqCount1 > 1 && seqCount2 == 1) {
-			
-			}
-			//Align a sequence to a group
-			else if(seqCount1 == 1 && seqCount2 > 1) {
-				
-			}
-			//Align 2 groups
-			else if((seqCount1 > 1) && (seqCount2 > 1)){
-					
-			}
-			else {
-				Console.traceln(Level.INFO,"No sequences to align while merging two nodes.");
-			}
-		}
-		else {
-			Console.traceln(Level.WARNING,"More than 2 children! This should never happen, it's a binary tree.");
-		}
-		return alignment;
-	}
-
 	public void setSequences(ArrayList<NumberSequence> alignSequences) {
 		this.sequences = alignSequences;
 	}
 	
+	
+	
 }
Index: branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/pal/tree/Node.java
===================================================================
--- branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/pal/tree/Node.java	(revision 1584)
+++ branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/pal/tree/Node.java	(revision 1585)
@@ -134,5 +134,5 @@
 	ArrayList<NumberSequence> getSequences();
 	
-	void joinChildren( int n1, int n2);
+	Node joinChildren( int n1, int n2);
 
 	void addSequence(NumberSequence numberSequence);
Index: branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/pal/tree/UPGMAAligningTree.java
===================================================================
--- branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/pal/tree/UPGMAAligningTree.java	(revision 1584)
+++ branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/pal/tree/UPGMAAligningTree.java	(revision 1585)
@@ -15,9 +15,14 @@
 
 import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.logging.Level;
 
 import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.NumberSequence;
+import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.SmithWatermanRepeated;
 import de.ugoe.cs.autoquest.tasktrees.alignment.matrix.BinaryAlignmentStorage;
+import de.ugoe.cs.autoquest.tasktrees.alignment.matrix.ObjectDistanceSubstitionMatrix;
 import de.ugoe.cs.autoquest.tasktrees.alignment.matrix.UPGMAMatrix;
 import de.ugoe.cs.autoquest.tasktrees.alignment.pal.misc.Identifier;
+import de.ugoe.cs.util.console.Console;
 
 
@@ -42,5 +47,5 @@
 	 * @param m distance matrix
 	 */
-	public UPGMAAligningTree(ArrayList<NumberSequence> numberseqs, BinaryAlignmentStorage alignments)
+	public UPGMAAligningTree(ArrayList<NumberSequence> numberseqs, BinaryAlignmentStorage alignments, ObjectDistanceSubstitionMatrix submat)
 	{
 		if (alignments.getDistanceMatrix().size() < 2)
@@ -51,4 +56,5 @@
 		this.numberseqs = numberseqs;
 		this.alignments = alignments;
+		this.submat = submat;
 		init(alignments.getDistanceMatrix());
 
@@ -76,4 +82,5 @@
 	private ArrayList<NumberSequence> numberseqs;
 	private BinaryAlignmentStorage alignments;
+	private ObjectDistanceSubstitionMatrix submat;
 	private int numClusters;
 	private int besti, abi;
@@ -107,4 +114,5 @@
 			Node tmp = NodeFactory.createNode();
 			tmp.setIdentifier(new Identifier(Integer.toString(i)));
+			tmp.setNumber(i);
 			tmp.addSequence(numberseqs.get(i));
 			getRoot().addChild(tmp);
@@ -181,5 +189,9 @@
 		
 		// Index besti now represent the new cluster
-		getRoot().joinChildren(besti, bestj);
+		Node newNode = getRoot().joinChildren(besti, bestj);
+		
+		if(newNode instanceof FengDoolittleNode) {
+			newNode.setSequences(alignSequences(newNode));
+		}
 		
 		// Update alias
@@ -190,4 +202,91 @@
 		
 		numClusters--;
+	}
+	
+	
+	public ArrayList<NumberSequence> alignSequences(Node parent) {
+		ArrayList<NumberSequence> alignment = new ArrayList<NumberSequence>();
+		if(parent.getChildCount()<3) {
+			
+			Node node1 = parent.getChild(0);
+			Node node2 = parent.getChild(1);
+			
+			int seqCount1 = node1.getSequences().size();
+			int seqCount2 = node2.getSequences().size();
+			Console.traceln(Level.INFO,"Merging node " + node1.getIdentifier() + " with " + node2.getIdentifier());
+			//Console.println("SeqCount1: " + seqCount1 + " seqCount2 " + seqCount2);
+			//Align 2 sequences
+			if(seqCount1 == 1 && seqCount2 == 1) {
+				alignment = (alignments.get(node1.getNumber(), node2.getNumber())).getAlignment();
+			}
+			//Align a sequence to a group
+			else if( seqCount1 > 1 && seqCount2 == 1) {
+				alignment.addAll(node1.getSequences());
+				
+				BinaryAlignmentStorage tempStorage = new BinaryAlignmentStorage(seqCount1,seqCount2);
+				double maxScore = 0.0;
+				int maxIndex = 0;
+				for(int i=0;i<seqCount1;i++) {
+					tempStorage.set(i, 1, new SmithWatermanRepeated(node1.getSequence(i).getSequence(), node2.getSequence(0).getSequence() , submat, 5));
+					if(maxScore < tempStorage.get(i, 1).getAlignmentScore()) {
+						maxScore = tempStorage.get(i, 1).getAlignmentScore();
+						maxIndex = i;
+					}
+				}
+				alignment.add(tempStorage.get(maxIndex, 1).getAlignment().get(1));
+			}
+			//Align a sequence to a group
+			else if(seqCount1 == 1 && seqCount2 > 1) {
+				alignment.addAll(node2.getSequences());
+				BinaryAlignmentStorage tempStorage = new BinaryAlignmentStorage(seqCount1,seqCount2);
+				double maxScore = 0.0;
+				int maxIndex = 0;
+				for(int i=0;i<seqCount2;i++) {
+					tempStorage.set(1, i, new SmithWatermanRepeated(node2.getSequence(i).getSequence(), node1.getSequence(0).getSequence() , submat, 5));
+					if(maxScore < tempStorage.get(1, i).getAlignmentScore()) {
+						maxScore = tempStorage.get(1, i).getAlignmentScore();
+						maxIndex = i;
+					}
+				}
+				alignment.add(tempStorage.get(1,maxIndex).getAlignment().get(1));
+			}
+			//Align 2 groups
+			else if((seqCount1 > 1) && (seqCount2 > 1)){
+					BinaryAlignmentStorage tempStorage1 = new BinaryAlignmentStorage(seqCount2,1);
+					BinaryAlignmentStorage tempStorage2 = new BinaryAlignmentStorage(seqCount1,1);
+					double maxScore1 = 0.0;
+					double maxScore2 = 0.0;
+					int maxIndex1 = 0;
+					int maxIndex2 = 0;
+					for(int i=0;i<seqCount1;i++) {
+						for(int j=0;j<seqCount2;j++) {
+							tempStorage1.set(j, 0, new SmithWatermanRepeated(node1.getSequence(i).getSequence(), node2.getSequence(j).getSequence() , submat, 5));
+							if(maxScore1 < tempStorage1.get(j, 0).getAlignmentScore()) {
+								maxScore1 = tempStorage1.get(j, 0).getAlignmentScore();
+								maxIndex1 = j;
+							}
+						}
+						alignment.add(tempStorage1.get(maxIndex1,0).getAlignment().get(0));
+					}
+					for(int i=0; i<seqCount2;i++) {
+						for (int j=0;j<seqCount1;j++) {
+							tempStorage2.set(j, 0, new SmithWatermanRepeated(node2.getSequence(i).getSequence(),node1.getSequence(j).getSequence(),submat,5));
+							if(maxScore2 < tempStorage2.get(j, 0).getAlignmentScore()) {
+								maxScore2 = tempStorage2.get(j, 0).getAlignmentScore();
+								maxIndex2 = j;
+							}
+						}
+						alignment.add(tempStorage2.get(maxIndex2,0).getAlignment().get(0));
+					}
+					
+			}
+			else {
+				Console.traceln(Level.WARNING,"No sequences to align while merging " + node1.getIdentifier() + " with " + node2.getIdentifier());
+			}
+		}
+		else {
+			Console.traceln(Level.WARNING,"More than 2 children! This should never happen, it's a binary tree.");
+		}
+		return alignment;
 	}
 
@@ -217,2 +316,4 @@
 	}
 }
+
+
