Added logic for leaving and automatic leaving on disconnection