Added error handling to the connection