 
 void setup() {
   lastCardScan = 0;
-  timeBetweenScans = 800; // TODO: Set to 1500 when in production
+  timeBetweenScans = 200;
   Serial.begin(19200); // Initialize serial communications with master vending arduino
-  Serial.setTimeout(400);
+  Serial.setTimeout(200);
   SPI.begin(); // Init SPI bus
   mfrc522.PCD_Init(); // Init MFRC522 card
-}
-
-#define transmission_attempts 10
-#define transmission_repeats 5
+  pinMode(9, OUTPUT); // speaker out
+}
+
+#define TRANSMISSION_ATTEMPTS 10
+#define TRANSMISSION_REPEATS 5
+#define TRANSMISSION_SPACE 95
+#define TRANSMISSION_ATOM_SIZE 3
 uint8_t recieve_error;
-uint16_t rfid_recieve(){
-  char parseBuffer[transmission_repeats*2];
-  uint8_t recieveAttemptsRemaining = transmission_attempts;
+
+uint16_t rfid_raw_read(){
+  char parseBuffer[TRANSMISSION_REPEATS*TRANSMISSION_ATOM_SIZE];
   uint16_t number = 0;
-  while(recieveAttemptsRemaining-- > 0){
-    if(Serial.readBytes(parseBuffer, sizeof(parseBuffer)) == sizeof(parseBuffer)){
-      // Recieved correct ammount of bytes - check message integrity
-      if(memcmp(parseBuffer,parseBuffer+2,sizeof(parseBuffer)-2) == 0){
+  recieve_error = 1; // set default to error
+  if(Serial.readBytes(parseBuffer, sizeof(parseBuffer)) == sizeof(parseBuffer)){
+    // Recieved correct ammount of bytes - check message integrity
+    if(memcmp(parseBuffer,parseBuffer+TRANSMISSION_ATOM_SIZE,sizeof(parseBuffer)-TRANSMISSION_ATOM_SIZE) == 0){
+      if(parseBuffer[2] == TRANSMISSION_SPACE){
         // OK
         recieve_error = 0;
         // Let other party know we do not need a retransmission
-        Serial.write(0);
         memcpy(&number,parseBuffer,sizeof(number));
-        return number;
       }
     }
-    // Recieved incorrect ammount of bytes - Retry only if we are going to run function again
-    if(recieveAttemptsRemaining > 0)
-      Serial.write(recieveAttemptsRemaining+48);
+  }
+  return number;
+}
+
+uint16_t rfid_recieve(unsigned char command){
+  char parseBuffer[TRANSMISSION_REPEATS*TRANSMISSION_ATOM_SIZE];
+  uint8_t recieveAttemptsRemaining = TRANSMISSION_ATTEMPTS;
+  uint16_t number = 0;
+  while(recieveAttemptsRemaining-- > 0){
+    // Flush incoming buffer
+    while(Serial.available()){Serial.read();delay(5);}
+    // Request and read data from other party
+    Serial.write(command);
+    number = rfid_raw_read();
+    if(recieve_error == 0){ // Recieved correctly, return value
+      return number;
+    }
+    delay(5);
+    // Recieved incorrect ammount of bytes, or an error occured
   }
   recieve_error = 1; // timed out
-  // Let other party know we do not need a retransmission
-  Serial.write(0);
   return number;
 }
 
-char rfid_transmit(uint16_t number){
-//uint16_t number;                      // 0001 0110 0100 0111
+char rfid_raw_transmit(uint16_t number){
+  //uint16_t number;                    // 0001 0110 0100 0111
   uint16_t mask   = B11111111;          // 0000 0000 1111 1111
   uint8_t first_half   = number >> 8;   // >>>> >>>> 0001 0110
   uint8_t sencond_half = number & mask; // ____ ____ 0100 0111
+  for (int i = 0; i < TRANSMISSION_REPEATS*TRANSMISSION_ATOM_SIZE; i = i+TRANSMISSION_ATOM_SIZE) {
+    Serial.write(sencond_half);
+    Serial.write(first_half);
+    Serial.write(TRANSMISSION_SPACE);
+  }
+}
+
+char rfid_transmit(unsigned char command, uint16_t number){
   char waiting_for_ok = 1;
-  uint8_t transmitAttemptsRemaining = transmission_attempts;
+  uint8_t transmitAttemptsRemaining = TRANSMISSION_ATTEMPTS;
   while(waiting_for_ok && transmitAttemptsRemaining-- > 0){
     // Flush incoming buffer
     while(Serial.available()){Serial.read();}
-    for (int i = 0; i < transmission_repeats*2; i = i+2) {
-      Serial.write(sencond_half);
-      Serial.write(first_half);
-    }
-    if(Serial.readBytes(&waiting_for_ok,1) == 0) // No bytes recieved
-      waiting_for_ok = 1; // Overwrite waiting_for_ok - necessary?
+    // Transmit data
+    Serial.write(command);
+    rfid_raw_transmit(number);
+    Serial.readBytes(&waiting_for_ok,1);
+    //if(Serial.readBytes(&waiting_for_ok,1) == 0) // No bytes recieved
+      //waiting_for_ok = 1; // Overwrite waiting_for_ok - necessary?
   }
   return waiting_for_ok;
 }

            // ERR Beep + display "NO CREDITS"?
            //Serial.println("NO CREDITS - KNOWN CARD");
            Serial.print("N");
            // Halt PICC
            mfrc522.PICC_HaltA();
            // Return

          if(updateAndVerify(addr,card)){
            // Check if card has been changed
            // OK Beep + Set credits on vending machine
            char error = rfid_transmit('S',current_credits);
            if(error != 0){
              // Could not update machine credits - attempt to save credits to card again!
              card.credits = current_credits;
              updateAndVerify(addr,card);
              BEEP(4);
            }// else {BEEP(1);}
          } else {
            // ERR Beep + display "ERR  EEPROM BAD" ?
            //Serial.println("ERR  EEPROM BAD");
            Serial.print("B");
            BEEP(4);
            // Halt PICC
            mfrc522.PICC_HaltA();
            // Return
