     return;
   }
 
-  if (mfrc522.uid.uidByte[0] == nuidPICC[0] &&
-      mfrc522.uid.uidByte[1] == nuidPICC[1] &&
-      mfrc522.uid.uidByte[2] == nuidPICC[2] &&
-      mfrc522.uid.uidByte[3] == nuidPICC[3] ) {
-    Serial.println(F("Card already read"));
-    return;
-  }
-
-  // Store NUID into nuidPICC array
-  for (byte i = 0; i < 4; i++) {
-    nuidPICC[i] = mfrc522.uid.uidByte[i];
-  }
-
   char folder[17];
-  readBlock(1, 0, folder);
-  Serial.print(F("Folder on rfid: "));
-  Serial.println(folder);
-  playFolder(folder);
-
-  delay(100);
-}
-
-void readBlock(uint8_t sector, uint8_t relativeBlock, char *outputBuffer) {
+  if (readRfidBlock(1, 0, folder)) {
+
+    // Store NUID into nuidPICC array
+    for (byte i = 0; i < 4; i++) {
+      nuidPICC[i] = mfrc522.uid.uidByte[i];
+    }
+
+    switchFolder(folder);
+    stopMp3();
+    yield();
+    playMp3();
+  }
+
+}
+
+boolean readRfidBlock(uint8_t sector, uint8_t relativeBlock, char *outputBuffer) {
   if (relativeBlock > 3) {
     Serial.println(F("Invalid block number"));
-    return;
+    return false;
   }
 
   // Block 4 is trailer block

  Serial.print(F("Using rfid key (for A and B):"));
  dump_byte_array(key.keyByte, MFRC522::MF_KEY_SIZE);
  Serial.println();
}

/**
   Main loop.
*/
void loop() {
#if defined(USE_MP3_REFILL_MEANS) \
      && ( (USE_MP3_REFILL_MEANS == USE_MP3_SimpleTimer) \
      ||   (USE_MP3_REFILL_MEANS == USE_MP3_Polled)      )

  MP3player.available();
#endif

  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent())
    return;

  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial())
    return;

  // Show some details of the PICC (that is: the tag/card)
  Serial.print(F("Card UID:"));

/**
 * Put this file https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WebServer/examples/SDWebServer/SdRoot/edit/index.htm
 * in the root folder of the SD card for webinterface
*/

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <SPI.h>
#include <MFRC522.h>
#include <Adafruit_VS1053.h>
#include <SD.h>

#define RC522_CS_PIN          D0
#define SD_CS_PIN             D2

#define BREAKOUT_RESET  D4     // VS1053 reset pin (output)
#define BREAKOUT_CS     D8     // VS1053 chip select pin (output)
#define BREAKOUT_DCS    D3    // VS1053 Data/command select pin (output)
#define DREQ            D1    // VS1053 Data request, ideally an Interrupt pin

Adafruit_VS1053_FilePlayer musicPlayer =
  Adafruit_VS1053_FilePlayer(BREAKOUT_RESET, BREAKOUT_CS, BREAKOUT_DCS, DREQ, SD_CS_PIN);

WiFiManager wifiManager;

ESP8266WebServer server(80);

MFRC522 mfrc522(RC522_CS_PIN, UINT8_MAX);   // Create MFRC522 instance.

MFRC522::MIFARE_Key key;

// Init array that will store new NUID
byte nuidPICC[4];

boolean playing = false;

File currentFolder;

File uploadFile;


void setup() {
  Serial.begin(115200);

  SPI.begin();        // Init SPI bus
  mfrc522.PCD_Init(); // Init MFRC522 card
