added support for reconnect and cleaned up a little bit