           }
           address[strlen(address) -4] = '\0';
           strncat(address, config.devices[i].address_segment[j].segment, 254 - strlen(address));
-          if (j < (ADDRESS_SEGMENTS -1) && strlen(config.devices[i].address_segment[j +1].segment) > 0) {
+          if (j < (ADDRESS_SEGMENTS -1) &&
+              strlen(config.devices[i].address_segment[j +1].segment) > 0) {
             strncat(address, "/_all", 254 - strlen(address));
           }
-          Serial.println(address);
-          mqtt_client.subscribe(address);
+          subscription_count = subscribe_one(address, subscriptions, subscription_count);
         }
       }
     }

    Serial.print((char)payload[i]);
  }
  Serial.println();

  // Switch on the LED if an 1 was received as first character
  if ((char)payload[0] == '1') {
    digitalWrite(led, LOW);   // Turn the LED on (Note that LOW is the voltage level
    // but actually the LED is on; this is because
    // it is acive low on the ESP-01)
  } else {
    digitalWrite(led, HIGH);  // Turn the LED off by making the voltage HIGH
  }
}

void mqtt_connect() {
  Broker broker = brokers.GetBroker();
  mqtt_client.setServer(broker.address, 1883);
  mqtt_client.setCallback(mqtt_callback);
  if (mqtt_client.connect(config.hostname)) {

WiFiClient espClient;
PubSubClient mqtt_client(espClient);

void parse_topic(char* topic, Address_Segment* address_segments){
  int segment = -2;  // We don't care about the first 2 segments.
  char* p_segment_start = topic;
  char* p_segment_end = strchr(topic, '/');
  while(p_segment_end != NULL){
    if(segment >= 0){
      int segment_len = p_segment_end - p_segment_start;
      if(segment_len > ADDRESS_SEGMENT_LEN){
        segment_len = ADDRESS_SEGMENT_LEN;
      }
      strncpy(address_segments[segment].segment, p_segment_start, segment_len);
      address_segments[segment].segment[segment_len] = '\0';
    }
    p_segment_start = p_segment_end +1;
    p_segment_end = strchr(p_segment_start, '/');
    segment++;
  }
  strncpy(address_segments[segment++].segment, p_segment_start, ADDRESS_SEGMENT_LEN);
  
  for(; segment < ADDRESS_SEGMENTS; segment++){
    address_segments[segment].segment[0] = '\0';
  }
}

bool compare_addresses(Address_Segment* address_1, Address_Segment* address_2){
  if(strlen(address_2[0].segment) <= 0){
    return false;
  }
  if(strcmp(address_1[0].segment, "_all") != 0 &&
      strcmp(address_1[0].segment, address_2[0].segment) != 0){
    return false;
  }
  for(int s=1; s < ADDRESS_SEGMENTS; s++){
    if(strcmp(address_1[s].segment, "_all") == 0){
      return true;
    }
    if(strcmp(address_1[s].segment, address_2[s].segment) != 0){
      return false;
    }
  }
  return true;
}

String value_from_payload(byte* payload, unsigned int length, String key) {
  key.trim();
  String buffer;
  int index = 0;
  for (int i = 0; i < length; i++) {
    buffer += (char)payload[i];
  }
  buffer.trim();
  
  while(buffer.length()){
    index = buffer.indexOf(",", index);
    if(index < 0){
      index = buffer.length();
    }
    String substring = buffer.substring(0, index);
    int seperator_pos = substring.indexOf(":");
    if(seperator_pos > 0){
      String k = substring.substring(0, seperator_pos);
      String v = substring.substring(seperator_pos +1);
      k.trim();
      v.trim();
      if(k == key){
        return v;
      }
    }

    buffer = buffer.substring(index +1);
    buffer.trim();
  }

  return "";
}

// Called whenever a MQTT topic we are subscribed to arrives.
void mqtt_callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
