Refactoring the bluetooth code