Index: applications/editors/josm/plugins/alignways/src/com/tilusnet/josm/plugins/alignways/AlignWaysAlgnSegment.java
===================================================================
--- applications/editors/josm/plugins/alignways/src/com/tilusnet/josm/plugins/alignways/AlignWaysAlgnSegment.java	(revision 33133)
+++ applications/editors/josm/plugins/alignways/src/com/tilusnet/josm/plugins/alignways/AlignWaysAlgnSegment.java	(revision 33136)
@@ -6,4 +6,5 @@
 import java.awt.Point;
 import java.awt.Shape;
+import java.awt.RenderingHints;
 import java.awt.geom.Ellipse2D;
 import java.awt.geom.Line2D;
@@ -38,5 +39,5 @@
     private enum PivotLocations {
         NONE, NODE1, NODE2, CENTRE
-    };
+    }
 
     private PivotLocations currPivot;
@@ -119,7 +120,7 @@
             switch (pp) {
             case NODE1:
-                return segment.way.getNode(segment.lowerIndex).getEastNorth();
+                return segment.getFirstNode().getEastNorth();
             case NODE2:
-                return segment.way.getNode(segment.lowerIndex + 1).getEastNorth();
+                return segment.getSecondNode().getEastNorth();
             case CENTRE:
                 n1 = getPivotCoord(PivotLocations.NODE1);
@@ -227,6 +228,12 @@
     @Override
     public void paint(Graphics2D g, MapView mv, Bounds bbox) {
-        // Note: segment should never be null here
         super.paint(g, mv, bbox);
+
+        // Note: segment should never be null here, and its nodes should never be missing.
+        // If they are, it's a bug, possibly related to tracking of DataSet deletions.
+        if (segment.way.getNodesCount() <= segment.lowerIndex + 1) {
+            Main.warn("Not drawing AlignWays pivot points: underlying nodes disappeared");
+            return;
+        }
 
         // Ensure consistency
@@ -247,4 +254,5 @@
         g.setColor(pivotColor);
         g.setStroke(new BasicStroke());
+        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 
         Shape pvCentrePoint = new Ellipse2D.Double(
@@ -272,4 +280,6 @@
         g.setColor(crossColor);
         g.setStroke(new BasicStroke());
+        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
+
         g.draw(crossV);
         g.draw(crossH);
Index: applications/editors/josm/plugins/alignways/src/com/tilusnet/josm/plugins/alignways/AlignWaysCmdKeepLength.java
===================================================================
--- applications/editors/josm/plugins/alignways/src/com/tilusnet/josm/plugins/alignways/AlignWaysCmdKeepLength.java	(revision 33133)
+++ applications/editors/josm/plugins/alignways/src/com/tilusnet/josm/plugins/alignways/AlignWaysCmdKeepLength.java	(revision 33136)
@@ -10,4 +10,5 @@
 import java.util.HashSet;
 import java.util.Map;
+import java.util.Collections;
 
 import javax.swing.Icon;
@@ -92,13 +93,9 @@
         this.displaceableNodes = algnSeg.getSegmentEndPoints();
 
-        EastNorth enRefNode1 = refWS.way.getNode(refWS.lowerIndex)
-                .getEastNorth();
-        EastNorth enRefNode2 = refWS.way.getNode(refWS.lowerIndex + 1)
-                .getEastNorth();
-
-        EastNorth enAlgnNode1 = algnWS.way.getNode(algnWS.lowerIndex)
-                .getEastNorth();
-        EastNorth enAlgnNode2 = algnWS.way.getNode(algnWS.lowerIndex + 1)
-                .getEastNorth();
+        EastNorth enRefNode1 = refWS.getFirstNode().getEastNorth();
+        EastNorth enRefNode2 = refWS.getSecondNode().getEastNorth();
+
+        EastNorth enAlgnNode1 = algnWS.getFirstNode().getEastNorth();
+        EastNorth enAlgnNode2 = algnWS.getSecondNode().getEastNorth();
 
         // Calculate the rotation angle
@@ -203,4 +200,11 @@
             modified.add(osm);
         }
+    }
+
+    @Override
+    public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
+        Collection<OsmPrimitive> prims = new HashSet<>(displaceableNodes);
+        prims.add(algnSeg.getSegment().way);
+        return Collections.unmodifiableCollection(prims);
     }
 
Index: applications/editors/josm/plugins/alignways/src/com/tilusnet/josm/plugins/alignways/AlignWaysDialog.java
===================================================================
--- applications/editors/josm/plugins/alignways/src/com/tilusnet/josm/plugins/alignways/AlignWaysDialog.java	(revision 33133)
+++ applications/editors/josm/plugins/alignways/src/com/tilusnet/josm/plugins/alignways/AlignWaysDialog.java	(revision 33136)
@@ -11,4 +11,5 @@
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.util.Objects;
 
 import javax.swing.BorderFactory;
@@ -117,8 +118,8 @@
     @Override
     public void actionPerformed(ActionEvent e) {
-        if (e.getActionCommand() == "awOptKeepLen") {
+        if (Objects.equals(e.getActionCommand(), "awOptKeepLen")) {
             awOpt = AligningModeOption.ALGN_OPT_KEEP_LENGTH;
             infoText.setText(tr("<html>Aligns the way segment to the reference so that its length is preserved.</html>"));
-        } else if (e.getActionCommand() == "awOptKeepAng") {
+        } else if (Objects.equals(e.getActionCommand(), "awOptKeepAng")) {
             awOpt = AligningModeOption.ALGN_OPT_KEEP_ANGLE;
             infoText.setText(tr("<html>Aligns the way segment to the reference so that the angles of its adjacent segments are preserved.<br/>" +
@@ -136,5 +137,5 @@
 
     /**
-     * @param action If set to true, the dialog will show the mode options, otherwise it will show some instructions
+     * @param activeMode If set to true, the dialog will show the mode options, otherwise it will show some instructions
      */
     public void activate(boolean activeMode) {
Index: applications/editors/josm/plugins/alignways/src/com/tilusnet/josm/plugins/alignways/AlignWaysMode.java
===================================================================
--- applications/editors/josm/plugins/alignways/src/com/tilusnet/josm/plugins/alignways/AlignWaysMode.java	(revision 33133)
+++ applications/editors/josm/plugins/alignways/src/com/tilusnet/josm/plugins/alignways/AlignWaysMode.java	(revision 33136)
@@ -21,7 +21,17 @@
 import org.openstreetmap.josm.actions.mapmode.MapMode;
 import org.openstreetmap.josm.data.osm.DataSet;
+import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent;
+import org.openstreetmap.josm.data.osm.event.DataChangedEvent;
+import org.openstreetmap.josm.data.osm.event.DataSetListener;
+import org.openstreetmap.josm.data.osm.event.NodeMovedEvent;
+import org.openstreetmap.josm.data.osm.event.PrimitivesAddedEvent;
+import org.openstreetmap.josm.data.osm.event.PrimitivesRemovedEvent;
+import org.openstreetmap.josm.data.osm.event.RelationMembersChangedEvent;
+import org.openstreetmap.josm.data.osm.event.TagsChangedEvent;
+import org.openstreetmap.josm.data.osm.event.WayNodesChangedEvent;
 import org.openstreetmap.josm.gui.IconToggleButton;
 import org.openstreetmap.josm.gui.MapFrame;
 import org.openstreetmap.josm.gui.layer.Layer;
+import org.openstreetmap.josm.gui.layer.OsmDataLayer;
 import org.openstreetmap.josm.tools.Shortcut;
 
@@ -31,5 +41,5 @@
  *
  */
-public class AlignWaysMode extends MapMode /* implements MapViewPaintable */{
+public class AlignWaysMode extends MapMode implements DataSetListener {
 
     private static final long serialVersionUID = -1090955708412011141L;
@@ -78,4 +88,7 @@
         awSegs = AlignWaysSegmentMgr.getInstance(Main.map.mapView);
         Main.map.mapView.addMouseListener(this);
+        DataSet ds = Main.getLayerManager().getEditDataSet();
+        if (ds != null)
+            ds.addDataSetListener(this);
         setCurrentState(noneSelected);
     }
@@ -95,4 +108,7 @@
         setCurrentState(noneSelected);
         Main.map.mapView.removeMouseListener(this);
+        DataSet ds = Main.getLayerManager().getEditDataSet();
+        if (ds != null)
+            ds.removeDataSetListener(this);
         AlignWaysPlugin.getAwAction().setEnabled(false);
     }
@@ -128,16 +144,29 @@
         }
 
-        // Activate the Align Ways button if we have enough selections
-        if (currentState == bothSelected) {
-            AlignWaysPlugin.getAwAction().setEnabled(true);
-        } else {
-            AlignWaysPlugin.getAwAction().setEnabled(false);
-        }
         Main.map.mapView.repaint();
     }
 
     /**
-     * @param currentState
-     *            One of the AlignWays states
+     * Sets the current state based on the selected segments.
+     * @param mgr AlignWays segment manager singleton
+     */
+    public void setCurrentState(AlignWaysSegmentMgr mgr) {
+
+        boolean algnSelected = mgr.getAlgnSeg() != null;
+        boolean refSelected = mgr.getRefSeg() != null;
+
+        if (algnSelected && refSelected)
+            setCurrentState(getBothSelected());
+        else if (algnSelected)
+            setCurrentState(getAligneeSelected());
+        else if (refSelected)
+            setCurrentState(getReferenceSelected());
+        else
+            setCurrentState(getNoneSelected());
+    }
+
+    /**
+     * Sets the current state.
+     * @param currentState One of the AlignWays states
      */
     public void setCurrentState(AlignWaysState currentState) {
@@ -145,7 +174,11 @@
         currentState.setHelpText();
 
+        // Activate the Align Ways button if we have enough selections
+        AlignWaysPlugin.getAwAction().setEnabled(currentState == bothSelected);
+
         if (currentState == noneSelected) {
             awSegs.cleanupWays();
-            // TODO getCurrentDataSet may return null when the editable layer had
+
+            // getEditDataSet() may return null when the editable layer had
             // already been removed by JOSM. This happens e.g. when the user closes
             // JOSM while AlignWays mode is still active.
@@ -183,11 +216,4 @@
     public AlignWaysState getBothSelected() {
         return bothSelected;
-    }
-
-    /**
-     * @return the current state
-     */
-    public AlignWaysState getCurrentState() {
-        return currentState;
     }
 
@@ -229,8 +255,52 @@
     @Override
     public boolean layerIsSupported(Layer l) {
-        if (l == null)
-            return false;
-        else
-            return true;
+        return l instanceof OsmDataLayer;
+    }
+
+    @Override
+    protected void updateEnabledState() {
+        setEnabled(getLayerManager().getEditLayer() != null);
+    }
+
+    /* --------------- *
+     * DataSetListener *
+     * --------------- */
+
+    @Override
+    public void primitivesAdded(PrimitivesAddedEvent event) {
+    }
+
+    @Override
+    public void primitivesRemoved(PrimitivesRemovedEvent event) {
+        awSegs = AlignWaysSegmentMgr.getInstance(Main.map.mapView);
+
+        // Check whether any of the removed primitives were part of a highlighted alignee or reference segment.
+        // If so: remove the affected segment and update the state accordingly.
+        if (awSegs.primitivesRemoved(event.getPrimitives()))
+            setCurrentState(awSegs);
+    }
+
+    @Override
+    public void tagsChanged(TagsChangedEvent event) {
+    }
+
+    @Override
+    public void nodeMoved(NodeMovedEvent event) {
+    }
+
+    @Override
+    public void wayNodesChanged(WayNodesChangedEvent event) {
+    }
+
+    @Override
+    public void relationMembersChanged(RelationMembersChangedEvent event) {
+    }
+
+    @Override
+    public void otherDatasetChange(AbstractDatasetChangedEvent event) {
+    }
+
+    @Override
+    public void dataChanged(DataChangedEvent event) {
     }
 }
Index: applications/editors/josm/plugins/alignways/src/com/tilusnet/josm/plugins/alignways/AlignWaysSegment.java
===================================================================
--- applications/editors/josm/plugins/alignways/src/com/tilusnet/josm/plugins/alignways/AlignWaysSegment.java	(revision 33133)
+++ applications/editors/josm/plugins/alignways/src/com/tilusnet/josm/plugins/alignways/AlignWaysSegment.java	(revision 33136)
@@ -10,4 +10,5 @@
 import java.awt.Graphics2D;
 import java.awt.Point;
+import java.awt.RenderingHints;
 import java.awt.geom.Line2D;
 import java.util.Collection;
@@ -19,4 +20,5 @@
 import org.openstreetmap.josm.data.osm.Node;
 import org.openstreetmap.josm.data.osm.OsmPrimitive;
+import org.openstreetmap.josm.data.osm.Way;
 import org.openstreetmap.josm.data.osm.WaySegment;
 import org.openstreetmap.josm.gui.MapView;
@@ -57,10 +59,7 @@
         if (segment != null) {
             try {
-                Node node1 = segment.way.getNode(segment.lowerIndex);
-                Node node2 = segment.way.getNode(segment.lowerIndex + 1);
-
                 segmentEndPoints = new HashSet<>();
-                segmentEndPoints.add(node1);
-                segmentEndPoints.add(node2);
+                segmentEndPoints.add(segment.getFirstNode());
+                segmentEndPoints.add(segment.getSecondNode());
             } catch (IndexOutOfBoundsException e) {
                 Main.error(e);
@@ -89,14 +88,18 @@
     @Override
     public void paint(Graphics2D g, MapView mv, Bounds bbox) {
+        // Note: segment should never be null here, and its nodes should never be missing.
+        // If they are, it's a bug, possibly related to tracking of DataSet deletions.
+        if (segment.way.getNodesCount() <= segment.lowerIndex + 1) {
+            Main.warn("Not drawing AlignWays highlighting segment: underlying nodes disappeared");
+            return;
+        }
+
         highlightSegment(segmentColor, g, mv);
     }
 
     protected void highlightSegment(Color c, Graphics2D g, MapView mv) {
-        if (segment.way.getNodesCount() == 0) {
-            return;
-        }
-
         g.setColor(c);
         g.setStroke(new BasicStroke(6, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
+        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
         drawSegment(g, mv);
     }
@@ -104,6 +107,6 @@
     protected void drawSegment(Graphics2D g, MapView mv) {
         try {
-            Node n1 = segment.way.getNode(segment.lowerIndex);
-            Node n2 = segment.way.getNode(segment.lowerIndex + 1);
+            Node n1 = segment.getFirstNode();
+            Node n2 = segment.getSecondNode();
 
             g.draw(new Line2D.Double(mv.getPoint(n1), mv.getPoint(n2)));
@@ -111,4 +114,13 @@
             Main.error(e);
         }
+    }
+
+    protected boolean containsPrimitive(OsmPrimitive primitive) {
+        if (segment == null)
+            return false;
+
+        return (primitive instanceof Way && segment.way.equals(primitive)) ||
+                (primitive instanceof Node && segmentEndPoints.contains(primitive));
+
     }
 
Index: applications/editors/josm/plugins/alignways/src/com/tilusnet/josm/plugins/alignways/AlignWaysSegmentMgr.java
===================================================================
--- applications/editors/josm/plugins/alignways/src/com/tilusnet/josm/plugins/alignways/AlignWaysSegmentMgr.java	(revision 33133)
+++ applications/editors/josm/plugins/alignways/src/com/tilusnet/josm/plugins/alignways/AlignWaysSegmentMgr.java	(revision 33136)
@@ -5,4 +5,5 @@
 import java.awt.Point;
 import java.util.Collection;
+import java.util.List;
 
 import javax.swing.JOptionPane;
@@ -10,4 +11,5 @@
 import org.openstreetmap.josm.Main;
 import org.openstreetmap.josm.data.osm.Node;
+import org.openstreetmap.josm.data.osm.OsmPrimitive;
 import org.openstreetmap.josm.gui.MapView;
 
@@ -31,13 +33,21 @@
     }
 
+    /**
+     * Get or creates the segment manager instance belonging to a given MapView.
+     */
     public static AlignWaysSegmentMgr getInstance(MapView mapView) {
-        if (singleton == null) {
-            synchronized (AlignWaysSegmentMgr.class) {
-                if (singleton == null) {
-                    singleton = new AlignWaysSegmentMgr(mapView);
-                }
+        synchronized (AlignWaysSegmentMgr.class) {
+            if (singleton == null || !singleton.getMapView().equals(mapView)) {
+                singleton = new AlignWaysSegmentMgr(mapView);
             }
         }
         return singleton;
+    }
+
+    /**
+     * @return The MapView this segment manager belongs to.
+     */
+    private MapView getMapView() {
+        return mv;
     }
 
@@ -49,40 +59,36 @@
     public boolean algnUpdate(Point clickedPoint) {
 
-        if (algnSeg != null) {
-            // Check first if there is a pivot point nearby that needs selection
-            if (algnSeg.updatePivot(clickedPoint))
-                // Updated pivot, alignee reference unchanged
-                return false;
+        // Check first if there is a pivot point nearby that needs selection
+        if (algnSeg != null && algnSeg.updatePivot(clickedPoint)) {
+            // Updated pivot, alignee reference unchanged
+            return false;
         }
 
         // Previous attempt of pivot update unsuccessful, check alignee update
-        AlignWaysAlgnSegment tmpAlgnSeg = new AlignWaysAlgnSegment(mv,
-                clickedPoint);
+        AlignWaysAlgnSegment tmpAlgnSeg = new AlignWaysAlgnSegment(mv, clickedPoint);
         if (tmpAlgnSeg.getSegment() == null)
             return false;
-        else {
-            // Found a segment
-            // It may happen that the new segment is identical with the already
-            // selected reference:
-            if ((refSeg != null) && (tmpAlgnSeg.equals(refSeg))) {
-                // This action is then ignored (we won't clear the reference
-                // segment)
-                JOptionPane.showMessageDialog(Main.parent,
-                        tr("Segment to be aligned cannot be the same with the reference segment.\n" +
-                        "Please choose a different segment to be aligned."),
-                        tr("AlignWayS message"), JOptionPane.WARNING_MESSAGE);
-                return false;
-            }
-            // This will be a new alignee, old alignee (if any) will be lost:
-            if (algnSeg != null) {
-                algnSeg.destroy();
-            }
 
-            // Update alignee
-            algnSeg = tmpAlgnSeg;
-
-            return true;
+        // Found a segment - it may happen that the new segment is identical with the already
+        // selected asignee or reference segment. This action is then ignored.
+        if (algnSeg != null && tmpAlgnSeg.equals(algnSeg)) {
+            return false;
+        }
+        else if (refSeg != null && tmpAlgnSeg.equals(refSeg)) {
+            JOptionPane.showMessageDialog(Main.parent,
+                    tr("Segment to be aligned cannot be the same with the reference segment.\n" +
+                    "Please choose a different segment to be aligned."),
+                    tr("AlignWayS message"), JOptionPane.WARNING_MESSAGE);
+            return false;
         }
 
+        // This will be a new alignee, old alignee (if any) will be lost:
+        if (algnSeg != null) {
+            algnSeg.destroy();
+        }
+
+        // Update alignee
+        algnSeg = tmpAlgnSeg;
+        return true;
     }
 
@@ -94,33 +100,29 @@
     public boolean refUpdate(Point clickedPoint) {
 
-        AlignWaysRefSegment tmpRefSeg = new AlignWaysRefSegment(mv,
-                clickedPoint);
-        // TODO Have to check what happens when refSeg wasn't null previously
+        AlignWaysRefSegment tmpRefSeg = new AlignWaysRefSegment(mv, clickedPoint);
         if (tmpRefSeg.getSegment() == null)
             return false;
-        else {
-            // Found a segment
-            // It may happen that the new segment is identical with the already
-            // selected alignee:
-            if ((algnSeg != null) && (tmpRefSeg.equals(algnSeg))) {
-                // This action is then ignored (we won't clear the alignee
-                // segment)
-                JOptionPane.showMessageDialog(Main.parent,
-                        tr("Reference segment cannot be the same with the segment to be aligned.\n" +
-                        "Please choose a different reference segment."),
-                        tr("AlignWayS message"), JOptionPane.WARNING_MESSAGE);
-                return false;
-            }
-            // This will be a new reference, old reference (if any) will be lost:
-            if (refSeg != null) {
-                refSeg.destroy();
-            }
 
-            // Update reference
-            refSeg = tmpRefSeg;
-            return true;
-
+        // Found a segment - it may happen that the new segment is identical with the already
+        // selected asignee or reference segment. This action is then ignored.
+        if (refSeg != null && refSeg.equals(tmpRefSeg)) {
+            return false;
+        }
+        else if (algnSeg != null && tmpRefSeg.equals(algnSeg)) {
+            JOptionPane.showMessageDialog(Main.parent,
+                    tr("Reference segment cannot be the same with the segment to be aligned.\n" +
+                    "Please choose a different reference segment."),
+                    tr("AlignWayS message"), JOptionPane.WARNING_MESSAGE);
+            return false;
         }
 
+        // This will be a new reference, old reference (if any) will be lost:
+        if (refSeg != null) {
+            refSeg.destroy();
+        }
+
+        // Update reference
+        refSeg = tmpRefSeg;
+        return true;
     }
 
@@ -159,3 +161,26 @@
     }
 
+
+    /**
+     * Handles the event that OSM primitives were removed and checks whether
+     * this invalidates the currently selected alignee or reference segment.
+     * @return Whether any of the selected segments were invalidated.
+     * @param primitives List of primitives that were removed.
+     */
+    public boolean primitivesRemoved(List<? extends OsmPrimitive> primitives) {
+        boolean changed = false;
+        for (OsmPrimitive p : primitives) {
+            if (algnSeg != null && algnSeg.containsPrimitive(p)) {
+                algnSeg.destroy();
+                algnSeg = null;
+                changed = true;
+            }
+            if (refSeg != null && refSeg.containsPrimitive(p)) {
+                refSeg.destroy();
+                refSeg = null;
+                changed = true;
+            }
+        }
+        return changed;
+    }
 }
