     }
     val += wtemp;
   }
+  
   if (j == 0)
   {
-    wtemp = -100;  
-  } else {
+    wtemp = -100;
+  }
+  else
+  {
     wtemp = val/j;
   }
 #endif // WATER_TEMPERATURE_SENSOR
+}
+
+int water_offline()
+{
+	waterTemperature.reset_search();
+    return (!waterTemperature.search(addr));
 }
 
 // Get the water temperature for a DS18B20 sensor
 float get_water_temperature() {
-  static byte addr[8];
   static byte data[12];
   static int16_t raw;
   static byte i;
   static byte cfg;
-
-  waterTemperature.reset_search();
-  if (!waterTemperature.search(addr)) {
-    // No sensors found.
-    return -100;
-  }
 
   waterTemperature.reset();
   waterTemperature.select(addr);


#if WATER_TEMPERATURE_SENSOR
    // Add water temperature
    len = strlen(buffer);
    buffer[len] = ';';
    len++;
    sprintf(&buffer[len], "Tw:");
    len = strlen(buffer);
    sprintf(&buffer[len], "C=");
    len = strlen(buffer);
    dtostrf(wtemp, 1, 1, &buffer[len]);
#endif // WATER_TEMPERATURE_SENSOR

    // Created message

  for(i = 0; i < 3; i++)
  {
    wtemp = get_water_temperature();
    if(water_offline())
    {
      Serial.println("Error with water temperature reading, trying again");
      wtemp = get_water_temperature();        
      if (water_offline())
      {
        Serial.println("Couldn't get water temperature");        
        wtemp = 0;
