Changes to avoid crashes when peripheral is suddenly disconnected