   {
     char c = SerialUSB.read();
 
-    if (c == '\n' || c == '\r')
-    {
-      serialcomplete = true;
+    if (c == '\n' || c == '\r' || c == 'X')
+    {
+      if (input != "")
+      {
+        serialcomplete = true;
+      }
       break;
     }
     input += c;


      // Wait atleast 40ns (P5A)
      delayMicroseconds(1);
      SerialUSB.write("Sent 4 command bits and 8 data payload bits\r\nResponse: ");
      SerialUSB.print(String(response, HEX) + "\r\n");

      return response;
    }

  }
}

// Bulk erase the entire device
void bulkErase()
{
  // Write 0F0F to 3C0005h
  writeAddressConfig(0x3C, 0x00, 0x05, 0x0F0F);
  // Write 8F8F to 3C0004h
  writeAddressConfig(0x3C, 0x00, 0x04, 0x8F8F);
  sendOp(B0000, 0x0000);
  sendOp(B0000, 0x0000);
  // Wait atleast 15ms (P11) to let the erase complete
  delay(20);
}

// Print device IDs on serial
void readDeviceID()
{
  int DEVID1 = readAddress(0x3F, 0xFF, 0xFE);
  int DEVID2 = readAddress(0x3F, 0xFF, 0xFF);
  SerialUSB.print("DEVID1: " + String(DEVID1, HEX) + "h\r\n");
  SerialUSB.print("DEVID2: " + String(DEVID2, HEX) + "h\r\n");
}

// Sets the TBLPTR to the address to be read/written
void setTBLPTR(int addru, int addrh, int addrl)
{
  sendOp(B0000, 0x0E00 + addru); // MOVLW <Addr[21:16]>
  sendOp(B0000, 0x6EF8);         // MOVWF TBLPTRU
  sendOp(B0000, 0x0E00 + addrh); // MOVLW <Addr[15:8]>
  sendOp(B0000, 0x6EF7);         // MOVWF TBLPTRH
  sendOp(B0000, 0x0E00 + addrl); // MOVLW <Addr[7:0]>
  sendOp(B0000, 0x6EF6);         // MOVWF TBLPTRL
}

// Read value at address
int readAddress(int addru, int addrh, int addrl)
{
  setTBLPTR(addru, addrh, addrl);
  return sendOp(B1001, 0x0000); // TBLRD *+
}

// Write value to address in config block
void writeAddressConfig(int addru, int addrh, int addrl, int value)
{
  setTBLPTR(addru, addrh, addrl);
  sendOp(B1100, value);
}

// Writes a sequence of bytes into the code memmory
// bytes.length <= 64, bytes[n] <= 0xFFFF
void writeCodeSequence(int addru, int addrh, int addrl, int bytes[])
{
  int LoopCount = 0;

  // Direct access to code memory
  sendOp(B0000, 0x8EA6); // BSF EECON1, EEPGD
  sendOp(B0000, 0x9CA6); // BCF EECON1, CFGS
  sendOp(B0000, 0x84A6); // BSF EECON1, WREN

  // Point to row to write.
  setTBLPTR(addru, addrh, addrl);

  // Load write buffer. Repeat for all but the last two bytes.
  for (int n = 0; n < SIZE - 2; n = n + 2)
  {
    sendOp(B1101, (bytes[n + 1] << 8) + bytes[n]);
  }

  // Load write buffer for last two bytes and start programming
  sendOp(B1111, (bytes[SIZE - 2] << 8) + bytes[SIZE - 1]);

  // Send NOP and Hold 4th PGC HIGH for atleast 1ms (P9)
  digitalWrite(PGD, LOW);
  for (int n = 0; n < 4; n++)
  {
    digitalWrite(PGC, LOW);
    delayMicroseconds(1);
    digitalWrite(PGC, HIGH);
    delayMicroseconds(1);
  }
  delay(100);

  // Hold PGC LOW for atleast 200us (P10)
  digitalWrite(PGC, LOW);
  delayMicroseconds(400);
}

void readTest()
{
  int response;
  for (int n = 0; n < SIZE; n++)
  {
    response = readAddress(0, 0, n);
    SerialUSB.print(String(response, HEX) + "\r\n");
  }
}

void loop()
