cleanup and bug fix for reconnecting