Refactored bluetooth code significantly