improve connection handling - still a mess but better
update a few comments