Added some error checking, specially when there are connection errors.