diff --git a/src/jswrap_process.c b/src/jswrap_process.c
index b25720f..6019960 100644
--- a/src/jswrap_process.c
+++ b/src/jswrap_process.c
@@ -156,9 +156,11 @@ JsVar *jswrap_process_env() {
   // Pointer to a list of predefined exports - eventually we'll get rid of the array above
   jsvObjectSetChildAndUnLock(obj, "EXPTR", jsvNewFromInteger((JsVarInt)(size_t)exportPtrs));
 #ifdef NRF5X
+  #ifdef BUETOOTH // Fix linker error
   extern uint32_t app_ram_base;
   if (app_ram_base)
     jsvObjectSetChildAndUnLock(obj, "APP_RAM_BASE", jsvNewFromInteger((JsVarInt)app_ram_base));
+  #endif
 #endif
 #endif
   return obj;
diff --git a/targets/nrf5x/jshardware.c b/targets/nrf5x/jshardware.c
index 879942b..7cca12c 100644
--- a/targets/nrf5x/jshardware.c
+++ b/targets/nrf5x/jshardware.c
@@ -55,6 +55,7 @@ USB data receive is broken, although examples+config seem almost identical.
 #include "bluetooth_utils.h"
 #include "jswrap_bluetooth.h"
 #else
+#include "nrf_sdm.h"
 #include "nrf_temp.h"
 void app_error_fault_handler(uint32_t id, uint32_t pc, uint32_t info) {
 }
@@ -93,6 +94,8 @@ void app_error_fault_handler(uint32_t id, uint32_t pc, uint32_t info) {
 #if NRF_SD_BLE_API_VERSION<5
 #include "softdevice_handler.h"
 #else
+#include "nrf_sdh.h"
+#include "nrf_sdh_soc.h"
 #include "nrfx_spim.h"
 #endif
 
@@ -984,7 +987,14 @@ void jshInit() {
 #else // !BLUETOOTH
   // because the code in bluetooth.c will call jsh_sys_evt_handler for us
   // if we were using bluetooth
+#ifdef SOFTDEVICE_PRESENT
+#if NRF_SD_BLE_API_VERSION<5
   softdevice_sys_evt_handler_set(jsh_sys_evt_handler);
+#else
+  NRF_SDH_SOC_OBSERVER(m_soc_observer, 1, jsh_sys_evt_handler, NULL);
+#endif
+#endif
+NRF_RTC0->TASKS_START = 1;
 #endif
 
   // Enable PPI driver
@@ -1002,8 +1012,8 @@ void jshInit() {
 
 #ifdef NRF_USB
   if (USBD_POWER_DETECTION) {
-    ret = app_usbd_power_events_enable();
-    APP_ERROR_CHECK(ret);
+    err_code = app_usbd_power_events_enable();
+    APP_ERROR_CHECK(err_code);
   } else {
     //jsiConsolePrintf("No USB power detection enabled\nStarting USB now\n");
     app_usbd_enable();
@@ -2478,10 +2488,14 @@ bool jshFlashErasePages(uint32_t addr, uint32_t byteLength) {
       jshFlashWriteProtect(startAddr + byteLength - 1))
     return false;
   uint32_t err;
+  uint8_t sd_enabled = 0;
+  #ifndef BLUETOOTH
+  sd_softdevice_is_enabled(&sd_enabled);
+  #endif
   while (byteLength>=4096 && !jspIsInterrupted()) {
     flashIsBusy = true;
     while ((err = sd_flash_page_erase(startAddr / NRF_FICR->CODEPAGESIZE)) == NRF_ERROR_BUSY);
-    if (err!=NRF_SUCCESS) flashIsBusy = false;
+    if (err!=NRF_SUCCESS || !sd_enabled) flashIsBusy = false;
     WAIT_UNTIL(!flashIsBusy, "jshFlashErasePage");
     /*if (err!=NRF_SUCCESS)
       jsiConsolePrintf("jshFlashErasePage got err %d at 0x%x\n", err, addr);*/
@@ -2643,7 +2657,10 @@ void jshFlashWrite(void * buf, uint32_t addr, uint32_t len) {
 #endif
   if (jshFlashWriteProtect(addr)) return;
   uint32_t err = 0;
-
+  uint8_t sd_enabled = 0;
+  #ifndef BLUETOOTH
+  sd_softdevice_is_enabled(&sd_enabled);
+  #endif
   if (((size_t)(char*)buf)&3) {
     /* Unaligned *SOURCE* is a problem on nRF5x,
      * so if so we are unaligned, do a whole bunch
@@ -2653,7 +2670,7 @@ void jshFlashWrite(void * buf, uint32_t addr, uint32_t len) {
       uint32_t alignedBuf;
       memcpy(&alignedBuf, buf, 4);
       while ((err = sd_flash_write((uint32_t*)addr, &alignedBuf, 1)) == NRF_ERROR_BUSY);
-      if (err!=NRF_SUCCESS) flashIsBusy = false;
+      if (err!=NRF_SUCCESS || !sd_enabled) flashIsBusy = false;
       WAIT_UNTIL(!flashIsBusy, "jshFlashWrite");
       len -= 4;
       addr += 4;
@@ -2673,7 +2690,7 @@ void jshFlashWrite(void * buf, uint32_t addr, uint32_t len) {
       while ((err = sd_flash_write(((uint32_t*)addr)+wordOffset, ((uint32_t *)buf)+wordOffset, l>>2)) == NRF_ERROR_BUSY && !jspIsInterrupted());
       wordOffset += l>>2;
     }
-    if (err!=NRF_SUCCESS) flashIsBusy = false;
+    if (err!=NRF_SUCCESS || !sd_enabled) flashIsBusy = false;
     WAIT_UNTIL(!flashIsBusy, "jshFlashWrite");
   }
   if (err!=NRF_SUCCESS)
