     if (client.connected())
       client.disconnect();
   }
-  static unsigned long tock = 0;
-
-  static doorstate_t lastdoorstate = CLOSED;
-  switch (doorstate) {
-    case CHECKINGCARD:
-      if (millis() - last_doorstatechange > 1500 && caching && checkCache(uid)) {
-        char msg[255];
-        snprintf(msg, sizeof(msg), "[%s] Allowing door to open based on cached information.", pname);
-        Serial.println(msg);
-        client.publish(log_topic, msg);
-        openDoor();
-      }
-      break;
-    case OPENING:
-      if (isOpen()) {
-        char msg[256];
-        snprintf(msg, sizeof(msg), "[%s] Door is open.", pname);
-        Serial.println(msg);
-        client.publish(log_topic, msg);
-        doorstate = OPEN;
-        cnt_opens++;
-      };
-      break;
-    case OPEN:
-      if (millis() - last_doorstatechange > DOOR_OPEN_DELAY) {
-        Serial.println("Closing door.");
-        closeDoor();
-      };
-    case CLOSING:
-      if (isClosed()) {
-        char msg[256];
-        snprintf(msg, sizeof(msg), "[%s] Door is closed.", pname);
-        Serial.println(msg);
-        client.publish(log_topic, msg);
-        doorstate = CLOSED;
-        cnt_closes++;
-      };
-      break;
-    case CLOSED:
-      if (!isClosed()) {
-        /// some alerting ? clearly a bug ?
-      }
-    default:
-      break;
-  };
-
-  if (lastdoorstate != doorstate) {
-    lastdoorstate = doorstate;
-    last_doorstatechange = millis();
-  }
-
+
+  {
+    static doorstate_t lastdoorstate = CLOSED;
+    switch (doorstate) {
+      case CHECKINGCARD:
+        if (millis() - last_doorstatechange > 1500 && caching && checkCache(uid)) {
+          char msg[255];
+          snprintf(msg, sizeof(msg), "[%s] Allowing door to open based on cached information.", pname);
+          Serial.println(msg);
+          client.publish(log_topic, msg);
+          openDoor();
+        }
+        break;
+      case OPENING:
+        if (isOpen()) {
+          char msg[256];
+          snprintf(msg, sizeof(msg), "[%s] Door is open.", pname);
+          Serial.println(msg);
+          client.publish(log_topic, msg);
+          doorstate = OPEN;
+          cnt_opens++;
+        };
+        break;
+      case OPEN:
+        if (millis() - last_doorstatechange > DOOR_OPEN_DELAY) {
+          Serial.println("Closing door.");
+          closeDoor();
+        };
+      case CLOSING:
+        if (isClosed()) {
+          char msg[256];
+          snprintf(msg, sizeof(msg), "[%s] Door is closed.", pname);
+          Serial.println(msg);
+          client.publish(log_topic, msg);
+          doorstate = CLOSED;
+          cnt_closes++;
+        };
+        break;
+      case CLOSED:
+        if (!isClosed()) {
+          /// some alerting ? clearly a bug ?
+        }
+      default:
+        break;
+    };
+
+    if (lastdoorstate != doorstate) {
+      lastdoorstate = doorstate;
+      last_doorstatechange = millis();
+    }
+  }
   // catch any logic errors or strange cases where the door is open while we think
   // we are not doing anything.
   //


MFRC522::Uid uid;

void callback(char* topic, byte * payload, unsigned int length) {
  char buff[256];


    cnt_fails ++;
  }

  {
    static unsigned long tock = 0;
    if (millis() - tock  > REPORTING_PERIOD) {
      cnt_minutes += ((millis() - tock) + 500) / 1000 / 60;
      reportStats();
      tock = millis();
    }
  }

  if (irqCardSeen) {
    if (mfrc522.PICC_ReadCardSerial()) {
      uid = mfrc522.uid;
      String uidStr = uidToString(uid);

      cnt_cards++;
      client.publish(rfid_topic, uidStr.c_str());

      char msg[256];
      snprintf(msg, sizeof(msg), "[%s] Tag <%s> (len=%d) swiped", pname, uidStr.c_str(), uid.size);
      client.publish(log_topic, msg);
      Serial.println(msg);

      doorstate = CHECKINGCARD;
    } else {
      Serial.println("Misread.");
