fixed code to be robust against WiFi problems