diff --git a/raspberry-backend/app.py b/raspberry-backend/app.py
index 9485b4ce645a63cf75c851966433ecaf5a0ea162..60976bf671b9000e694a0a420af3a9556b34eaa4 100755
--- a/raspberry-backend/app.py
+++ b/raspberry-backend/app.py
@@ -545,6 +545,8 @@ def last_data(rowid):
                 for index, item in enumerate(readback_variables):
                     data[item] = el[index]
                 fetched_all.append(data)
+
+    fetched_all.sort(key=lambda x : x["ROWID"]) # force rows into acending order
     response = make_response(json.dumps(fetched_all).encode('utf-8') )
     response.content_type = 'application/json'
     return response
diff --git a/raspberry-backend/static/js/Chart-displayLoop.js b/raspberry-backend/static/js/Chart-displayLoop.js
index f093d15e8d6250dc73fa755c3aead0a000aff033..0c8bb92e07636d131ac9b572afe3a4eba1957470 100644
--- a/raspberry-backend/static/js/Chart-displayLoop.js
+++ b/raspberry-backend/static/js/Chart-displayLoop.js
@@ -1,3 +1,19 @@
+// to quote the AAMI:
+//For Pressure-Volume loops, the graph
+//is required to use delivered volume on the vertical axis
+//and airway pressure on the horizontal axis. Positive
+//values should increase in up/right directions. Every
+//breath resets the graph, setting the volume back at the
+//origin.
+
+//For Flow-Volume loops the graph is required to use flow
+//rate on the vertical axis and delivered volume on the
+//horizontal axis. Positive values should increase in the
+//up/right directions.  Every breath resets the graph,
+//setting the volume back at the origin. The document also
+//suggests that there could be another version using
+//exhalation flow, if possible.
+
 //3 loop displays: Pressure-Volume, Flow-Volume and Pressure-Flow.
 var chart_PV;
 var chart_FV;
@@ -6,6 +22,10 @@ var chart_PF;
 // last row accessed from the database
 var last_row_accessed = 0;
 
+// is the plot in the exhale part of the cycle
+var inExhaleFill = false;
+var inExhale = false;
+
 var holdLoop = false; // global if loop plot to stop at end of this loop
 var stopLoop = false; // global if loop plotting is stopped
 
@@ -13,7 +33,7 @@ var stopLoop = false; // global if loop plotting is stopped
 function HoldLoop() {
     console.info("Holding loop");
     holdLoop = true;
-    stopLoop = false; // run on either to end of loop
+    stopLoop = false; // run to end of loop
     // toggle button state to allow a start or stop of the loop
     document.getElementById("LoopHoldButton").disabled = true;
     document.getElementById("LoopStartButton").disabled = false;
@@ -21,7 +41,7 @@ function HoldLoop() {
 function RunLoop() {
     console.info("Running loop");
     holdLoop = false;
-    stopLoop = false; // run on either to end of loop
+    stopLoop = false; // running again
     // toggle button state to allow a start or stop of the loop
     document.getElementById("LoopHoldButton").disabled = false;
     document.getElementById("LoopStartButton").disabled = true;
@@ -35,7 +55,6 @@ function RunLoop() {
     chart_PF.update();
 }
 
-
 /**
  * Request new data from the server, add it to the graph and set a timeout
  * to request again
@@ -46,12 +65,24 @@ function requestChartVar() {
         success: function(data) {
 	    if (data.length > 0 ) {
 		last_row_accessed = data[0]['ROWID'];
+
 		if (stopLoop) {
 		    return; // nothing to do if stopped
 		}
 		for ( let i = data.length-1 ; i >= 0; i--) {
 		    var point = data[i];
-		    if( point["fsm_state"] == 14 ) { // start of loop FIXME
+		    if( point["payload_type"] != "DATA" ) continue; // ignore all other data packets
+		    // loop could be INHALE -> PAUSE -> EXHALE_FILL -> INHALE -> EXHALE -> BUFF_PRE_INHALE -> new loop
+		    // may miss some of the shorter steps (PAUSE & BUFF_PRE_INHALE)
+		    if( point["fsm_state"] == "EXHALE_FILL" ){ 
+			inExhaleFill = true;  // in exhale part of loop
+		    }
+		    if( point["fsm_state"] == "EXHALE" ){ 
+			inExhale = true;  // in exhale part of loop
+		    }
+		    if( point["fsm_state"] == "INHALE" && inExhale ) { // start of loop (exhale->inhale transition)
+			inExhale = false;
+			inExhaleFill = false;
 			if( holdLoop ) {
 			    stopLoop = true;
 			    holdLoop = false;
@@ -69,45 +100,48 @@ function requestChartVar() {
 			    chart_PV.data.datasets[0].data.length = 0;
 			    chart_VF.data.datasets[0].data.length = 0;
 			    chart_PF.data.datasets[0].data.length = 0;
+
+			    chart_PV.data.datasets[1].data.length = 0;
+			    chart_VF.data.datasets[1].data.length = 0;
+			    chart_PF.data.datasets[1].data.length = 0;
 			}
-		    }else if( chart_PV.data.datasets[0].data.length > 100 ){
-			chart_PV.data.datasets[0].data.shift();
-			chart_VF.data.datasets[0].data.shift();
-			chart_PF.data.datasets[0].data.shift();
 		    }
-		    if( ! stopLoop ){ // skip updates if loop is stopped
-			//var pressure =  point["airway_pressure"];
+		    if( chart_PV.data.datasets[0].data.length > 1000 ){ // protect against not seeing a new loop
+			chart_PV.data.datasets[0].data.length = 100;
+			chart_VF.data.datasets[0].data.length = 100;
+			chart_PF.data.datasets[0].data.length = 100;
+			console.warning("Too many points to plot in inhale, is loop stopped?")
+		    }
+		    if( chart_PV.data.datasets[1].data.length > 1000 ){ // protect against not seeing a new loop
+			chart_PV.data.datasets[1].data.length = 100;
+			chart_VF.data.datasets[1].data.length = 100;
+			chart_PF.data.datasets[1].data.length = 100;
+			console.warning("Too many points to plot in exhale, is loop stopped?")
+		    }
+		    if( ! stopLoop ){
+			// if the loop is running update the points
 			var pressure =  point["pressure_patient"];
 			var volume = point["volume"];
 			var flow = point["flow"];
 
-			// to quote the AAMI:
-			//For Pressure-Volume loops, the graph
-			//is required to use delivered volume on the vertical axis
-			//and airway pressure on the horizontal axis. Positive
-			//values should increase in up/right directions. Every
-			//breath resets the graph, setting the volume back at the
-			//origin.
-
-			//For Flow-Volume loops the graph is required to use flow
-			//rate on the vertical axis and delivered volume on the
-			//horizontal axis. Positive values should increase in the
-			//up/right directions.  Every breath resets the graph,
-			//setting the volume back at the origin. The document also
-			//suggests that there could be another version using
-			//exhalation flow, if possible.
-
 			// loops:
-			chart_PV.data.datasets[0].data.push({x: pressure, y: volume});
-			chart_VF.data.datasets[0].data.push({x: volume, y: flow});
-			chart_PF.data.datasets[0].data.push({x: pressure, y: flow});
-
-			// now run chart updates
-			chart_PV.update();
-			chart_VF.update();
-			chart_PF.update();
+			if( (! inExhale) && (!inExhaleFill) ) {
+			    // inhale points
+			    chart_PV.data.datasets[0].data.push({x: pressure, y: volume});
+			    chart_VF.data.datasets[0].data.push({x: volume, y: flow});
+			    chart_PF.data.datasets[0].data.push({x: pressure, y: flow});
+			}else{
+			    // exhale points
+			    chart_PV.data.datasets[1].data.push({x: pressure, y: volume});
+			    chart_VF.data.datasets[1].data.push({x: volume, y: flow});
+			    chart_PF.data.datasets[1].data.push({x: pressure, y: flow});
+			}
 		    }
 		}
+		// now run chart updates: outside loop (only need to update plot every 0.2s)
+		chart_PV.update();
+		chart_VF.update();
+		chart_PF.update();
 	    }
 	},
         cache: false
@@ -122,11 +156,17 @@ $(document).ready(function() {
     chart_PV = new Chart(ctx_PV, {
         type: 'scatter',
         data: {datasets: [{data: [],
-			   label: "Pressure - Volume",
+			   label: "Inhale",
 			   borderColor: "rgb(51,99,255)",
 			   pointBackgroundColor : "rgb(51,99,255)",
 			   fill: false,
-			   showLine: true },
+			   showLine: false },
+			  {data: [],
+			   label: "Exhale : Pressure - Volume",
+			   borderColor: "rgb(255,99,51)",
+			   pointBackgroundColor : "rgb(255,99,51)",
+			   fill: false,
+			   showLine: false }
 			  ]},
 	options: {elements: { point: { radius: 5}},
 		  legend: { display: true, labels: {fontSize: 24 } },
@@ -144,11 +184,17 @@ $(document).ready(function() {
     chart_VF = new Chart(ctx_VF, {
         type: 'scatter',
         data: {datasets: [{data: [],
-			   label: "Volume - Flow",
+			   label: "Inhale",
 			   borderColor: "rgb(51,99,255)",
 			   pointBackgroundColor : "rgb(51,99,255)",
 			   fill: false,
-			   showLine: true }]}
+			   showLine: false },
+			  {data: [],
+			   label: "Exhale : Volume - Flow",
+			   borderColor: "rgb(255,99,51)",
+			   pointBackgroundColor : "rgb(255,99,51)",
+			   fill: false,
+			   showLine: false }]}
 	,options: {elements: { point: { radius: 5}},
 		  legend: { display: true, labels: {fontSize: 24 } },
 		scales: {xAxes: [{display: true,
@@ -165,11 +211,17 @@ $(document).ready(function() {
     chart_PF = new Chart(ctx_PF, {
         type: 'scatter',
         data: {datasets: [{data: [],
-			   label: "Pressure - Flow",
+			   label: "Inhale",
 			   borderColor: "rgb(51,99,255)",
 			   pointBackgroundColor : "rgb(51,99,255)",
 			   fill: false,
-			   showLine: true }]}
+			   showLine: false },
+			  {data: [],
+			   label: "Exhale : Pressure - Flow",
+			   borderColor: "rgb(255,99,51)",
+			   pointBackgroundColor : "rgb(255,99,51)",
+			   fill: false,
+			   showLine: false }]}
 	,options: {elements: { point: { radius: 5, fill: true}},
 		  legend: { display: true, labels: {fontSize: 24 } },
 		   scales: {xAxes: [{display: true,