Added error checking code

Connection fails at line 36