Fix critical bug in disconnect code