     return;
   }
 
-  if (settings.flags & FLAG_SESSION_VARS_SET) {
+  if ((settings.flags & FLAG_SESSION_VARS_SET)==FLAG_SESSION_VARS_SET) {
     debugPrint("Session vars set - initializing lora");
     loraSetSessionKeys(settings.seq_no, settings.AppSKey, settings.NwkSKey, settings.DevAddr);
 
     reportSessionVars();
   }
-  else if ((settings.flags & FLAG_APP_KEY_SET) && (settings.flags & FLAG_APP_EUI_SET) && (settings.flags & FLAG_DEV_EUI_SET)) {
+  else if ((settings.flags & FLAG_JOIN_VARS_SET)==FLAG_JOIN_VARS_SET) {
     debugPrint("Join keys set - initializing lora join");
     loraJoin(settings.seq_no, settings.AppKey, settings.AppEUI, settings.DevEUI, onJoin);
   }


  uint32_t seq_no;

#define FLAG_SESSION_VARS_SET (1 << 0) // Set of DevAddr, NwkSKey, and AppSKey set.
  u1_t DevAddr[4];
  u1_t NwkSKey[16];
  u1_t AppSKey[16];

// Separate set flags because BLE can't send all at once
#define FLAG_APP_KEY_SET (1 << 1)

  }
}

#define AssignSessionCallback(key, flag) \
void assign##key##Callback(uint8_t data[], uint16_t len) { \
  debugLogData("assign" #key, data, len); \
  if (len==sizeof(settings.key)) {        \
    memcpy(settings.key, data, sizeof(settings.key)); \
    saveSettingBytes(offset(settings, key), settings.key, sizeof(settings.key)); \
    settings.flags |= flag; \
    saveSettingValue(offset(settings, flags), settings.flags); \
    if ((settings.flags & FLAG_SESSION_VARS_SET)==FLAG_SESSION_VARS_SET) { \
      loraSetSessionKeys(settings.seq_no, settings.AppSKey, settings.NwkSKey, settings.DevAddr); \
    } \
  } \
}

AssignSessionCallback(DevAddr, FLAG_DEV_ADDR_SET)
AssignSessionCallback(NwkSKey, FLAG_NWK_SKEY_SET)
AssignSessionCallback(AppSKey, FLAG_APP_SKEY_SET)

#define AssignAppCallback(key, flag) \
void assign##key##Callback(uint8_t data[], uint16_t len) { \
