Refactored code. Added utilities to verify if device has a valid network connection