Index: include/urjtag/flash.h
===================================================================
--- include/urjtag/flash.h	(revision 1960)
+++ include/urjtag/flash.h	(working copy)
@@ -41,6 +41,8 @@
 
 typedef struct URJ_FLASH_CFI_ARRAY urj_flash_cfi_array_t;
 
+typedef int (*urj_flash_detect_func_t) (urj_bus_t *bus, uint32_t adr,  urj_flash_cfi_array_t **cfi_array);
+
 typedef struct
 {
     const char *name;
Index: src/flash/detectflash.c
===================================================================
--- src/flash/detectflash.c	(revision 1960)
+++ src/flash/detectflash.c	(working copy)
@@ -47,6 +47,17 @@
 
 urj_flash_cfi_array_t *urj_flash_cfi_array = NULL;
 
+static const int (*urj_flash_detect_funcs[]) (urj_bus_t *bus, uint32_t adr, 
+    urj_flash_cfi_array_t **cfi_array) = {
+    &urj_flash_cfi_detect,
+    &urj_flash_jedec_detect,
+    &urj_flash_amd_detect,
+#ifdef JEDEC_EXP
+    &urj_flash_jedec_exp_detect,
+#endif
+};
+
+
 void
 urj_flash_cleanup (void)
 {
@@ -59,6 +70,7 @@
 {
     urj_flash_cfi_query_structure_t *cfi;
     const char *s;
+    int i;
 
     if (!bus)
     {
@@ -72,26 +84,11 @@
 
     URJ_BUS_PREPARE (bus);
 
-    if (urj_flash_cfi_detect (bus, adr, &urj_flash_cfi_array) != URJ_STATUS_OK)
-    {
-        urj_flash_cleanup();
-        if (urj_flash_jedec_detect (bus, adr, &urj_flash_cfi_array)
-            != URJ_STATUS_OK)
-        {
-            urj_flash_cleanup();
-            if (urj_flash_amd_detect (bus, adr, &urj_flash_cfi_array)
-                != URJ_STATUS_OK)
-            {
-                urj_flash_cleanup();
-#ifdef JEDEC_EXP
-                if (urj_flash_jedec_exp_detect (bus, adr, &urj_flash_cfi_array)
-                    != URJ_STATUS_OK)
-                {
-                    urj_flash_cleanup();
-                }
-#endif
-            }
+    for (i=0; i < ARRAY_SIZE (urj_flash_detect_funcs); i++) {
+        if (urj_flash_detect_funcs[i] (bus, adr, &urj_flash_cfi_array) == URJ_STATUS_OK) {
+            break;
         }
+        urj_flash_cleanup ();
     }
 
     if (urj_flash_cfi_array == NULL)
