Refactor Bluetooth code to remove brittleness