Various fixes. Macro-ize device. Add debug logging.