Better handling of reconnect - fixed possible memory leak