Index: /applications/editors/josm/plugins/pt_assistant/src/org/openstreetmap/josm/plugins/pt_assistant/actions/FixTask.java
===================================================================
--- /applications/editors/josm/plugins/pt_assistant/src/org/openstreetmap/josm/plugins/pt_assistant/actions/FixTask.java	(revision 32274)
+++ /applications/editors/josm/plugins/pt_assistant/src/org/openstreetmap/josm/plugins/pt_assistant/actions/FixTask.java	(revision 32274)
@@ -0,0 +1,116 @@
+package org.openstreetmap.josm.plugins.pt_assistant.actions;
+
+import static org.openstreetmap.josm.tools.I18n.tr;
+
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
+import java.util.Collection;
+
+import javax.swing.SwingUtilities;
+
+import org.openstreetmap.josm.Main;
+import org.openstreetmap.josm.command.Command;
+import org.openstreetmap.josm.data.validation.TestError;
+import org.openstreetmap.josm.gui.PleaseWaitRunnable;
+import org.openstreetmap.josm.gui.progress.ProgressMonitor;
+import org.openstreetmap.josm.io.OsmTransferException;
+import org.xml.sax.SAXException;
+
+/**
+ * This class was copied with minor changes from ValidatorDialog.FixTask
+ * 
+ * @author darya
+ *
+ */
+public class FixTask extends PleaseWaitRunnable { 
+	
+	private final Collection<TestError> testErrors;
+	private boolean canceled;
+
+	public FixTask(Collection<TestError> testErrors) {
+		super(tr("Fixing errors ..."), false /* don't ignore exceptions */);
+		this.testErrors = testErrors == null ? new ArrayList<TestError>() : testErrors;
+	}
+
+	@Override
+	protected void cancel() {
+		this.canceled = true;
+	}
+
+	@Override
+	protected void finish() {
+		// do nothing
+	}
+
+	protected void fixError(TestError error) throws InterruptedException, InvocationTargetException {
+		if (error.isFixable()) {
+			final Command fixCommand = error.getFix();
+			if (fixCommand != null) {
+				SwingUtilities.invokeAndWait(new Runnable() {
+					@Override
+					public void run() {
+						Main.main.undoRedo.addNoRedraw(fixCommand);
+					}
+				});
+			}
+			// It is wanted to ignore an error if it said fixable, even if
+			// fixCommand was null
+			// This is to fix #5764 and #5773:
+			// a delete command, for example, may be null if all concerned
+			// primitives have already been deleted
+			error.setIgnored(true);
+		}
+	}
+
+	@Override
+	protected void realRun() throws SAXException, IOException, OsmTransferException {
+		ProgressMonitor monitor = getProgressMonitor();
+		try {
+			monitor.setTicksCount(testErrors.size());
+			int i = 0;
+			SwingUtilities.invokeAndWait(new Runnable() {
+				@Override
+				public void run() {
+					Main.main.getCurrentDataSet().beginUpdate();
+				}
+			});
+			try {
+				for (TestError error : testErrors) {
+					i++;
+					monitor.subTask(tr("Fixing ({0}/{1}): ''{2}''", i, testErrors.size(), error.getMessage()));
+					if (this.canceled)
+						return;
+					fixError(error);
+					monitor.worked(1);
+				}
+			} finally {
+				SwingUtilities.invokeAndWait(new Runnable() {
+					@Override
+					public void run() {
+						Main.main.getCurrentDataSet().endUpdate();
+					}
+				});
+			}
+			monitor.subTask(tr("Updating map ..."));
+			SwingUtilities.invokeAndWait(new Runnable() {
+				@Override
+				public void run() {
+					Main.main.undoRedo.afterAdd();
+					Main.map.repaint();
+					// tree.resetErrors();
+					Main.main.getCurrentDataSet().fireSelectionChanged();
+				}
+			});
+		} catch (InterruptedException | InvocationTargetException e) {
+			// FIXME: signature of realRun should have a generic checked
+			// exception we
+			// could throw here
+			throw new RuntimeException(e);
+		} finally {
+			monitor.finishTask();
+		}
+		
+	}
+
+}
Index: /applications/editors/josm/plugins/pt_assistant/src/org/openstreetmap/josm/plugins/pt_assistant/validation/PTAssitantValidatorTest.java
===================================================================
--- /applications/editors/josm/plugins/pt_assistant/src/org/openstreetmap/josm/plugins/pt_assistant/validation/PTAssitantValidatorTest.java	(revision 32273)
+++ /applications/editors/josm/plugins/pt_assistant/src/org/openstreetmap/josm/plugins/pt_assistant/validation/PTAssitantValidatorTest.java	(revision 32274)
@@ -20,4 +20,5 @@
 import org.openstreetmap.josm.data.validation.TestError;
 import org.openstreetmap.josm.gui.dialogs.relation.sort.RelationSorter;
+import org.openstreetmap.josm.plugins.pt_assistant.actions.FixTask;
 import org.openstreetmap.josm.plugins.pt_assistant.actions.IncompleteMembersDownloadThread;
 import org.openstreetmap.josm.plugins.pt_assistant.gui.IncompleteMembersDownloadDialog;
@@ -30,5 +31,5 @@
 	public static final int ERROR_CODE_ROAD_TYPE = 3721;
 	public static final int ERROR_CODE_DIRECTION = 3731;
-
+	
 	public PTAssitantValidatorTest() {
 		super(tr("Public Transport Assistant tests"),
@@ -57,8 +58,9 @@
 		this.errors.addAll(wayChecker.getErrors());
 
-		if (!this.errors.isEmpty()) {
+		if (this.errors.isEmpty()) {
+			proceedWithSorting(r);
+		} else {
 			this.proceedAfterWayCheckerErrors(r);
 		}
-
 
 	}
@@ -116,7 +118,5 @@
 
 		if (userInput == 0) {
-			for (TestError e : this.errors) {
-				fixError(e);
-			}
+			this.fixErrorFromPlugin(this.errors);
 			proceedWithSorting(r);
 			return;
@@ -130,4 +130,5 @@
 
 		if (userInput == 2) {
+			// TODO: should the errors be removed from the error list?
 			proceedWithSorting(r);
 		}
@@ -137,9 +138,48 @@
 
 	}
-	
+
+	/**
+	 * Carries out the second stage of the testing: sorting
+	 * @param r
+	 */
 	private void proceedWithSorting(Relation r) {
+		
 		// Check if the relation is correct, or only has a wrong sorting order:
 		RouteChecker routeChecker = new RouteChecker(r, this);
-		this.errors.addAll(routeChecker.getErrors());
+		List<TestError> routeCheckerErrors = routeChecker.getErrors();
+		
+		/*- At this point, there are 3 variants: 
+		 * 
+		 * 1) There are no errors => route is correct
+		 * 2) There is only a sorting error (can only be 1), but otherwise
+		 * correct.
+		 * 3) There are some other errors/gaps that cannot be fixed by
+		 * sorting => start further test (stop-by-stop) 
+		 * 
+		 * */
+		
+		
+		if (!routeCheckerErrors.isEmpty()) {
+			// Variant 2
+			// If there is only the sorting error, add it and stop testing.
+			this.errors.addAll(routeChecker.getErrors());
+			return;
+		}
+		
+		if (!routeChecker.getHasGap()) {
+			// Variant 1
+			// TODO: add the segments of this route to the list correct route segments		
+		}
+		
+		// Variant 3:
+		proceedAfterSorting(r);
+		
+		
+	}
+	
+	
+	private void proceedAfterSorting(Relation r) {
+		// TODO
+		performDummyTest(r);
 	}
 
@@ -158,15 +198,15 @@
 	@Override
 	public Command fixError(TestError testError) {
-		
-		List<Command> commands = new ArrayList<>(50);
-		
+
+		List<Command> commands = new ArrayList<>();
+
 		if (testError.getCode() == ERROR_CODE_DIRECTION || testError.getCode() == ERROR_CODE_ROAD_TYPE) {
 			commands.add(fixErrorByRemovingWay(testError));
 		}
-		
+
 		if (testError.getCode() == ERROR_CODE_SORTING) {
 			commands.add(fixSortingError(testError));
 		}
-		
+
 		if (commands.isEmpty()) {
 			return null;
@@ -180,12 +220,39 @@
 	}
 
+	/**
+	 * This method is the counterpart of the fixError(TestError testError)
+	 * method. The fixError method is invoked from the core validator (e.g. when
+	 * user presses the "Fix" button in the validator). This method is invoken
+	 * when the fix is initiated from within the plugin (e.g. automated fixes).
+	 * 
+	 * @return
+	 */
+	private void fixErrorFromPlugin(List<TestError> testErrors) {
+
+			
+			// run fix task asynchronously
+			FixTask fixTask = new FixTask(testErrors);
+//			Main.worker.submit(fixTask);
+			
+			Thread t = new Thread(fixTask);
+			t.start();
+			try {
+				t.join();
+				errors.removeAll(testErrors);
+
+			} catch (InterruptedException e) {
+				JOptionPane.showMessageDialog(null, "Error occurred during fixing");
+			}
+
+
+
+	}
 
 	private Command fixErrorByRemovingWay(TestError testError) {
-		
-	
+
 		if (testError.getCode() != ERROR_CODE_ROAD_TYPE && testError.getCode() != ERROR_CODE_DIRECTION) {
 			return null;
 		}
-		
+
 		List<OsmPrimitive> primitives = (List<OsmPrimitive>) testError.getPrimitives();
 		Relation originalRelation = (Relation) primitives.get(0);
@@ -236,5 +303,4 @@
 		ChangeCommand changeCommand = new ChangeCommand(originalRelation, modifiedRelation);
 
-		
 		return changeCommand;
 	}
Index: /applications/editors/josm/plugins/pt_assistant/src/org/openstreetmap/josm/plugins/pt_assistant/validation/RouteChecker.java
===================================================================
--- /applications/editors/josm/plugins/pt_assistant/src/org/openstreetmap/josm/plugins/pt_assistant/validation/RouteChecker.java	(revision 32273)
+++ /applications/editors/josm/plugins/pt_assistant/src/org/openstreetmap/josm/plugins/pt_assistant/validation/RouteChecker.java	(revision 32274)
@@ -5,4 +5,6 @@
 import java.util.ArrayList;
 import java.util.List;
+
+import javax.swing.JOptionPane;
 
 import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
@@ -24,8 +26,10 @@
 	// relation that is checked:
 	private Relation relation;
-	
+
 	// stores all found errors (on way level):
 	private ArrayList<TestError> errors = new ArrayList<>();
 	
+	private boolean hasGap;
+
 	List<RelationMember> sortedMembers;
 
@@ -35,4 +39,6 @@
 		this.relation = r;
 		
+		this.hasGap = false;
+
 		performSortingTest();
 
@@ -52,6 +58,9 @@
 			return;
 		}
-		
+
 		if (hasGap(waysToCheck)) {
+			
+			this.hasGap = true;
+			
 			RelationSorter sorter = new RelationSorter();
 			sortedMembers = sorter.sortMembers(waysToCheck);
@@ -59,5 +68,6 @@
 			if (!hasGap(sortedMembers)) {
 				TestError e = new TestError(this.test, Severity.WARNING,
-						tr("PT: Route contains a gap that can be fixed by sorting"), PTAssitantValidatorTest.ERROR_CODE_SORTING, relation);
+						tr("PT: Route contains a gap that can be fixed by sorting"),
+						PTAssitantValidatorTest.ERROR_CODE_SORTING, relation);
 				this.errors.add(e);
 
@@ -65,9 +75,7 @@
 
 		}
-		
-		
 
 	}
-	
+
 	/**
 	 * Checks if there is a gap for a given list of ways. It does not check if
@@ -76,5 +84,6 @@
 	 * 
 	 * @param waysToCheck
-	 * @return true if has gap (in the sense of continuity of ways in the Relation Editor), false otherwise
+	 * @return true if has gap (in the sense of continuity of ways in the
+	 *         Relation Editor), false otherwise
 	 */
 	private boolean hasGap(List<RelationMember> waysToCheck) {
@@ -93,13 +102,22 @@
 		return false;
 	}
-	
+
+	/**
+	 * Returns errors
+	 */
 	public List<TestError> getErrors() {
 
 		return errors;
 	}
+
+	public List<RelationMember> getSortedMembers() {
+
+		return sortedMembers;
+
+	}
 	
-	public List<RelationMember> getSortedMembers() {
+	public boolean getHasGap() {
 		
-		return sortedMembers;
+		return this.hasGap;
 		
 	}
