Added error handling for failed connections