Adding a few checks for a valid network connection. Hat tip @nbradbury.