       return;  // we can fail to parse a sentence in which case we should just wait for another
   }
  
-     if (XBee.available()) {
-      byte receivedPayload = XBee.read();
-      switch (receivedPayload) {
-        case FLAG_PAYLOAD:
-          digitalWrite(PIN_PAYLOAD,HIGH);
-          break;
-        default:
+  //handle XBee received data
+  if (Serial1.available()) {
+    byte receivedPayload = Serial1.read();
+    switch (receivedPayload) {
+      case FLAG_PAYLOAD:
+        if ( digitalRead(PIN_PAYLOAD) == HIGH )
           digitalWrite(PIN_PAYLOAD,LOW);
-          break;
-      }
-      #ifdef readDebug
-        Serial.print("size: ");
-        Serial.println(XBee.available());
-        Serial.println(receivedPayload, HEX);
-      #endif
+        else digitalWrite(PIN_PAYLOAD,HIGH);
+        break;
+      default:
+        digitalWrite(PIN_PAYLOAD,LOW);
+        break;
     }
+    #ifdef readDebug
+      Serial.print("read size: ");
+      Serial.println(Serial.available());
+      Serial.println(receivedPayload, HEX);
+    #endif
+  }
+  
   // if millis() or timer wraps around, reset
   if (timer > millis())  timer = millis();
   if (millis() - timer > 400) {

      //buffer size is the difference between the current and start pointers
      buf_size = buf_curr - buf_start;

      XBee.write(buf_start, buf_size);
      //arduino is little endian (LSB first)
      
      #ifdef writeDebug

          bufferReader++;
        }
        Serial.println();
      #endif
  }
}
