Index: applications/editors/josm/plugins/tracer/src/tracer/TracerAction.java
===================================================================
--- applications/editors/josm/plugins/tracer/src/tracer/TracerAction.java	(revision 19856)
+++ applications/editors/josm/plugins/tracer/src/tracer/TracerAction.java	(revision 19857)
@@ -39,4 +39,5 @@
 import org.xml.sax.SAXException;
 import org.openstreetmap.josm.data.osm.BBox;
+import org.openstreetmap.josm.data.osm.OsmPrimitive;
 
 /**
@@ -153,17 +154,29 @@
     }
 
+    private boolean isBuilding(Way w){
+        return (w.getKeys().get("building") == null ? false : w.getKeys().get("building").equals("yes"));
+    }
+
+    private boolean isInBuilding(Node n){
+        for(OsmPrimitive op :n.getReferrers())
+            if(op instanceof Way)
+                if(isBuilding((Way) op))
+                    return true;
+            return false;
+    }
+
+
     private Command connectObjects(Way way){
             List<Command> cmds = new LinkedList<Command>();
             Way newWay = new Way(way);
-            for (int i = 0; i < newWay.getNodesCount(); i++) {
-                Node n = newWay.getNode(i);
+            for (int i = 1; i < way.getNodesCount(); i++) {
+                Node n = way.getNode(i);
                 if (cancel) {
                     return null;
                 }
 
-                try {
                     LatLon ll = n.getCoor();
 
-                    double minDistanceSq = 0.00001;
+                    double minDistanceSq = 0.000015;
                     List<Node> nodes = Main.main.getCurrentDataSet().searchNodes(new BBox(
                             ll.getX() - minDistanceSq,
@@ -174,5 +187,5 @@
                     Node nearestNode = null;
                     for (Node nn : nodes) {
-                        if (!nn.isUsable() || way.containsNode(nn) || newWay.containsNode(nn)) {
+                        if (!nn.isUsable() || way.containsNode(nn) || newWay.containsNode(nn) || !isInBuilding(nn)) {
                             continue;
                         }
@@ -184,4 +197,6 @@
                     }
 
+                    //System.out.println("Nearest: " + nearestNode);
+                    //System.out.println("-------");
                     if (nearestNode == null) {
                         // hledani blizke usecky, kam bod pridat ... nebo vytvorit samostatny bod?
@@ -198,5 +213,5 @@
 
                         for (Way ww : ways) {
-                            if (!ww.isUsable() || ww == way || ww == newWay) {
+                            if (!ww.isUsable() || ww == way || ww == newWay || !isBuilding(ww)) {
                                 continue;
                             }
@@ -214,10 +229,13 @@
                         }
 
-                        if (minDist < 0.00001) {
+                        //System.out.println("Nearest way:" + nearestWay);
+                        if (minDist < 0.000015) {
                             Way newNWay = new Way(nearestWay);
                             newNWay.addNode(nearestNodeIndex + 1, n);
+                            //System.out.println("New way:" + newNWay);
                             cmds.add(new ChangeCommand(nearestWay, newNWay));
                         }
                     } else {
+                          nearestNode.setCoor(ll.getCenter(nearestNode.getCoor()));
                           int j = newWay.getNodes().indexOf(n);
                           newWay.addNode(j, nearestNode);
@@ -225,13 +243,7 @@
                           newWay.removeNode(n);
                           cmds.add(new DeleteCommand(n));
-                          i--;
                     }
-
-                } catch (Exception ex) {
-                    ex.printStackTrace();
-                }
-            }
-            /**/
-            // projdi kazdou novou usecku a zjisti, zda by nemela vest pres existujici body          
+            }
+            // projdi kazdou novou usecku a zjisti, zda by nemela vest pres existujici body
             int i = 0;
             while (i < newWay.getNodesCount()) {
@@ -245,7 +257,8 @@
                 LatLon n1 = newWay.getNodes().get(i).getCoor();
                 LatLon n2 = newWay.getNodes().get((i + 1) % newWay.getNodesCount()).getCoor();
-
-                double minDistanceSq = 0.00001;
-                double maxAngle = 10;
+                //System.out.println(newWay.getNodes().get(i) + "-----" + newWay.getNodes().get((i + 1) % newWay.getNodesCount()));
+
+                double minDistanceSq = 0.000015;
+                double maxAngle = 15;
                 List<Node> nodes = Main.main.getCurrentDataSet().searchNodes(new BBox(
                         Math.min(n1.getX(), n2.getX()) - minDistanceSq,
@@ -256,5 +269,5 @@
                 Node nearestNode = null;
                 for (Node nod : nodes) {
-                    if (!nod.isUsable() || way.containsNode(nod) || newWay.containsNode(nod)) {
+                    if (!nod.isUsable() || way.containsNode(nod) || newWay.containsNode(nod) || !isInBuilding(nod)) {
                         continue;
                     }
@@ -277,6 +290,6 @@
                 }
 
-                System.out.println("Nearest_: " + nearestNode);
-                System.out.println("");
+                //System.out.println("Nearest_: " + nearestNode);
+                //System.out.println("");
                 if (nearestNode == null) {
                     // tato usecka se nerozdeli
@@ -291,5 +304,5 @@
             }
 
-            cmds.add(new ChangeCommand(way, newWay));
+                cmds.add(new ChangeCommand(way, newWay));
 
             Command cmd = new SequenceCommand(tr("Merge objects nodes"), cmds);
@@ -338,8 +351,8 @@
             commands.add(new AddCommand(way));
             if(!ctrl) commands.add(connectObjects(way));
-
+            
             if (!commands.isEmpty()) {
                 Main.main.undoRedo.add(new SequenceCommand(tr("Tracer building"), commands));
-                
+
                 if(shift) Main.main.getCurrentDataSet().addSelected(way);
                 else Main.main.getCurrentDataSet().setSelected(way);
