Fix bug with unable to disconnect cleanly