code logic errors with device disconnect event