Fixed nullpointer dereference and refactored the reconnection code a bit so error reporting is more helpful