   Serial.println(" bytes free");
   Serial.flush();
   delay(2000);
-  conn.stop();  
+}
+
+void connectToServer() {
+  Serial.println("Connecting to the server.");
+  Serial.flush();
+  while(!conn.connect(server, port)) {
+    Serial.println("Can't connect to the server.");
+    Serial.flush();
+    conn.stop();
+    delay(1000);     
+  }
 }
 
 void setupWifi() {
-  initWifi();
-
-  Serial.println("\nWifi initialized. Connecting to server.");
-  printWifiStatus();
-
-  Serial.println("connected to server");
-  Serial.flush();
-}
-
-void connectToServer() {
-  while(!conn.connect(server, port)) {
-      Serial.println("Can't connect to the server.");
-      Serial.flush();
-      delay(1000);     
-    }
-}
-
-void initWifi() {
   int status = WL_IDLE_STATUS;
 
   while (status != WL_CONNECTED) {
     Serial.print("Attempting to connect to SSID: ");
     Serial.println(ssid);
     Serial.flush();
+    status = WiFi.begin(ssid, password);
+  }
 
-    status = WiFi.begin(ssid, password);
-
-    delay(3000);
-  }
-}
-
-void printWifiStatus() {
   // print the SSID of the network you're attached to:
   Serial.print("SSID: ");
   Serial.println(WiFi.SSID());

}

void loop() {
  connectToServer();
  Serial.println("writing");
  Serial.flush();
  pins.clear();
  for(int i = 0; i < 80; i++) {
    pins.push_back(Pin(i,NULL, i));
  }
  Serial.println("pushed a lot of pins into a vector");
  int written = pseudopod->writeStateMessage(pins);
  Serial.print(written);
  Serial.println(" bytes written.");
  Serial.print(freeMemory());

  Serial.begin(9600);
  Serial.println("Starting up.");
  setupWifi();
  pseudopod = new Pseudopod(conn, conn);
}

