Add unit tests, improve (re)connect