fixed a few bugs with leaving and closing connection