diff --git menu_site_map.module menu_site_map.module
index d853ef6..37daf68 100755
--- menu_site_map.module
+++ menu_site_map.module
@@ -1,7 +1,7 @@
 <?php
 // $Id: menu_site_map.module,v 1.1.2.1 2009/11/30 15:50:05 ghing Exp $
 
-/** 
+/**
  * @file
  *
  * Implementation of module hooks and utility functions for the Menu Site Map module.
@@ -33,7 +33,7 @@ function menu_site_map_menu() {
     'page callback' => 'drupal_get_form',
     'page arguments' => array('menu_site_map_settings_form'),
     'access arguments' => array('administer site configuration'),
-    'file' => 'menu_site_map.admin.inc',  
+    'file' => 'menu_site_map.admin.inc',
     'type' => MENU_NORMAL_ITEM,
   );
 
@@ -43,7 +43,7 @@ function menu_site_map_menu() {
     'page callback' => 'drupal_get_form',
     'page arguments' => array('menu_site_map_settings_form'),
     'access arguments' => array('administer site configuration'),
-    'file' => 'menu_site_map.admin.inc',  
+    'file' => 'menu_site_map.admin.inc',
     'type' => MENU_DEFAULT_LOCAL_TASK,
     'weight' => 0,
   );
@@ -54,7 +54,7 @@ function menu_site_map_menu() {
     'page callback' => 'drupal_get_form',
     'page arguments' => array('menu_site_map_select_menu_links_form'),
     'access arguments' => array('administer site configuration'),
-    'file' => 'menu_site_map.admin.inc',  
+    'file' => 'menu_site_map.admin.inc',
     'weight' => 1,
   );
 
@@ -93,7 +93,7 @@ function menu_site_map_theme() {
       'arguments' => array('menu_tree' => NULL, 'max_depth' => NULL),
     ),
     'menu_site_map_menu_tree' => array(
-      'arguments' => array('menu_tree' => NULL, 'current_depth' => NULL, 
+      'arguments' => array('menu_tree' => NULL, 'current_depth' => NULL,
                            'max_depth' => NULL),
     ),
     'menu_site_map_item_link' => array(
@@ -108,7 +108,7 @@ function menu_site_map_theme() {
  *
  * This is just a wrapper for the internal theme functions.
  */
-function theme_menu_site_map_item_container_open($depth, $is_first = FALSE, $leaf = FALSE) { 
+function theme_menu_site_map_item_container_open($depth, $is_first = FALSE, $leaf = FALSE) {
   $output = '';
   $display_style = variable_get('menu_site_map_display_style', 'default');
   if ($display_style == 'lists') {
@@ -129,18 +129,18 @@ function theme_menu_site_map_item_container_open($depth, $is_first = FALSE, $lea
 }
 
 /**
- * Theme the opening markeup a menu item's container in such a way that the 
+ * Theme the opening markeup a menu item's container in such a way that the
  * first and second levels are wrapped in divs.
  */
-function _theme_menu_site_map_item_container_open_default($depth, $is_first = FALSE, $leaf = FALSE) { 
+function _theme_menu_site_map_item_container_open_default($depth, $is_first = FALSE, $leaf = FALSE) {
   $output = '';
   if ($depth == 0) {
-    $output .= sprintf("<div class=\"container level-%s%s\">\n", 
+    $output .= sprintf("<div class=\"container level-%s%s\">\n",
                         $depth,
                         $is_first ? ' first' : '');
   }
   else if ($depth == 1) {
-    $output .= sprintf("<div class=\"container level-%s%s\">\n", 
+    $output .= sprintf("<div class=\"container level-%s%s\">\n",
                         $depth,
                         $is_first ? ' first' : '');
   }
@@ -152,19 +152,19 @@ function _theme_menu_site_map_item_container_open_default($depth, $is_first = FA
   return $output;
 }
 
-function _theme_menu_site_map_item_container_open_lists($depth, $is_first = FALSE, $leaf = FALSE) { 
+function _theme_menu_site_map_item_container_open_lists($depth, $is_first = FALSE, $leaf = FALSE) {
   $output = '';
- 
+
   if (!$leaf) {
-    $output .= sprintf("<li class=\"level-%s%s\">\n", 
+    $output .= sprintf("<li class=\"level-%s%s\">\n",
                        $depth,
-                       $is_first ? ' first' : ''); 
+                       $is_first ? ' first' : '');
   }
 
   return $output;
 }
 
-/** 
+/**
  * Theme the closing markup for a menu item's container.
  *
  * This is just a wrapper for the internal theme functions.
@@ -190,7 +190,7 @@ function theme_menu_site_map_item_container_close($depth, $leaf = FALSE) {
 }
 
 /**
- * Theme the closing markeup a menu item's container in such a way that the 
+ * Theme the closing markeup a menu item's container in such a way that the
  * first and second levels are wrapped in divs.
  */
 function _theme_menu_site_map_item_container_close_default($depth, $leaf = FALSE) {
@@ -248,10 +248,10 @@ function theme_menu_site_map_children_container_open($depth) {
 }
 
 /**
- * Default theming for the opening markup for the container of a menu item's 
- * children. 
+ * Default theming for the opening markup for the container of a menu item's
+ * children.
  *
- * The first and second level's children are not wrapped (as 
+ * The first and second level's children are not wrapped (as
  * the heading and container output by _theme_menu_site_map_children_container_open_default()
  * handles this).  The third and fourth level menus are output as lists.
  */
@@ -319,10 +319,10 @@ function theme_menu_site_map_children_container_close($depth) {
 }
 
 /**
- * Default theming for the closing markup for the container of a menu item's 
- * children. 
+ * Default theming for the closing markup for the container of a menu item's
+ * children.
  *
- * The first and second level's children are not wrapped (as 
+ * The first and second level's children are not wrapped (as
  * the heading and container output by _theme_menu_site_map_children_container_close_default()
  * handles this).  The third and fourth level menus are output as lists.
  */
@@ -401,13 +401,13 @@ function _theme_menu_site_map_item_default($link, $depth, $leaf=FALSE) {
                    $depth,
                    $leaf ? " leaf" : "");
   if ($depth == 0) {
-    $output .= sprintf("<h2 class=\"%s\">%s</h2>\n", $class, $link); 
+    $output .= sprintf("<h2 class=\"%s\">%s</h2>\n", $class, $link);
   }
   else if ($depth == 1) {
-    $output .= sprintf("<h3 class=\"%s\">%s</h3>\n", $class, $link); 
+    $output .= sprintf("<h3 class=\"%s\">%s</h3>\n", $class, $link);
   }
   else if ($depth >= 2) {
-    $output .= sprintf("<li class=\"%s\">%s</li>\n", $class, $link); 
+    $output .= sprintf("<li class=\"%s\">%s</li>\n", $class, $link);
   }
 
   return $output;
@@ -424,13 +424,13 @@ function _theme_menu_site_map_item_lists($link, $depth, $leaf=FALSE) {
   }
 
   if ($depth == 0) {
-    $output .= $link; 
+    $output .= $link;
   }
   else if ($depth == 1) {
-    $output .= $link; 
+    $output .= $link;
   }
   else if ($depth >= 2) {
-    $output .= $link; 
+    $output .= $link;
   }
 
   if ($leaf) {
@@ -468,7 +468,7 @@ function theme_menu_site_map_menu($menu_tree, $max_depth) {
     // Default
     $output .= _theme_menu_site_map_menu_default($menu_tree, $max_depth);
   }
-  
+
   return $output;
 }
 
@@ -490,7 +490,7 @@ function _theme_menu_site_map_menu_default($menu_tree, $max_depth) {
  */
 function _theme_menu_site_map_menu_lists($menu_tree, $max_depth) {
   $output = '';
-  
+
   $output .= "\n";
   $output .= "<ul class=\"menu-site-map menu-site-map-lists\">\n";
   $output .= theme('menu_site_map_menu_tree', $menu_tree, 0, $max_depth);
@@ -503,7 +503,7 @@ function _theme_menu_site_map_menu_lists($menu_tree, $max_depth) {
  * Theme a menu tree for output in a site map.
  */
 function theme_menu_site_map_menu_tree($menu_tree, $current_depth, $max_depth) {
-  $output = ''; 
+  $output = '';
   $delayed_output = '';
 
   if ($current_depth > $max_depth) {
@@ -512,52 +512,54 @@ function theme_menu_site_map_menu_tree($menu_tree, $current_depth, $max_depth) {
   else {
     $is_first = TRUE;
 
-    foreach ($menu_tree as $menu) {
-      $smm_item = db_fetch_array(db_query("SELECT * FROM {menu_site_map_links} WHERE mlid = %d", $menu['link']['mlid']));  
-      if (!$smm_item || $smm_item['included']) {
-        // We haven't excluded this menu item from our site map.
-
-        // Is this menu item as deep as we're going to go down our menu tree?
-        // This would be either because the item has no children or descending
-        // further down the tree would result in exceeding the max depth.
-        $leaf = !$menu['link']['has_children'] || $current_depth + 1 > $max_depth;
-
-        $output .= theme('menu_site_map_item_container_open', $current_depth, $is_first, $leaf);
-
-        $link = theme('menu_site_map_item_link', $menu['link']);
-        $item_output = theme('menu_site_map_item', 
-                             $link, 
-                             $current_depth, $leaf);
-        $move_second_level_leafs = variable_get('menu_site_map_move_second_level_leafs', FALSE);
-        if ($current_depth == 1 && !$menu['link']['has_children'] &&
-            $move_second_level_leafs == TRUE) {
-          // Put all single-item second level menus at the end, after those with children
-          $delayed_output .= $item_output; 
+    if (is_array($menu_tree)) {
+      foreach ($menu_tree as $menu) {
+        $smm_item = db_fetch_array(db_query("SELECT * FROM {menu_site_map_links} WHERE mlid = %d", $menu['link']['mlid']));
+        if (!$smm_item || $smm_item['included']) {
+          // We haven't excluded this menu item from our site map.
+
+          // Is this menu item as deep as we're going to go down our menu tree?
+          // This would be either because the item has no children or descending
+          // further down the tree would result in exceeding the max depth.
+          $leaf = !$menu['link']['has_children'] || $current_depth + 1 > $max_depth;
+
+          $output .= theme('menu_site_map_item_container_open', $current_depth, $is_first, $leaf);
+
+          $link = theme('menu_site_map_item_link', $menu['link']);
+          $item_output = theme('menu_site_map_item',
+                               $link,
+                               $current_depth, $leaf);
+          $move_second_level_leafs = variable_get('menu_site_map_move_second_level_leafs', FALSE);
+          if ($current_depth == 1 && !$menu['link']['has_children'] &&
+              $move_second_level_leafs == TRUE) {
+            // Put all single-item second level menus at the end, after those with children
+            $delayed_output .= $item_output;
+          }
+          else {
+            $output .= $item_output;
+          }
+
+          if ($menu['link']['has_children']) {
+            $output .= theme('menu_site_map_children_container_open', $current_depth + 1);
+            $output .= theme('menu_site_map_menu_tree', $menu['below'], $current_depth + 1, $max_depth);
+            $output .= theme('menu_site_map_children_container_close', $current_depth + 1);
+          }
+
+          $output .= theme('menu_site_map_item_container_close', $current_depth, $leaf);
+
+          if ($is_first) {
+            $is_first = FALSE;
+          }
         }
-        else {  
-          $output .= $item_output; 
-        }
-
-        if ($menu['link']['has_children']) {
-          $output .= theme('menu_site_map_children_container_open', $current_depth + 1);
-          $output .= theme('menu_site_map_menu_tree', $menu['below'], $current_depth + 1, $max_depth);
-          $output .= theme('menu_site_map_children_container_close', $current_depth + 1);
-        }
-
-        $output .= theme('menu_site_map_item_container_close', $current_depth, $leaf);
+      } // foreach
+    }
 
-        if ($is_first) {
-          $is_first = FALSE;
-        }
-      }
-    } // foreach
-   
     $output .= $delayed_output;
 
 
     return $output;
   }
-} 
+}
 
 function menu_site_map_tree_all_data($menu_name) {
   $menu_tree = menu_tree_all_data($menu_name);
@@ -567,11 +569,11 @@ function menu_site_map_tree_all_data($menu_name) {
   }
   $altered_menu_tree = $menu_tree;
   $menu_alterations = array();
-  $result = db_query("SELECT * FROM {menu_site_map_links} WHERE menu_name = '%s'", $menu_name);  
+  $result = db_query("SELECT * FROM {menu_site_map_links} WHERE menu_name = '%s'", $menu_name);
   while ($item = db_fetch_array($result)) {
     $menu_alterations[$item['mlid']] = $item;
   }
-   
+
   return $altered_menu_tree;
 }
 
