diff --git a/og_subgroups.common.inc b/og_subgroups.common.inc
index f228eed..c7b4e4b 100644
--- a/og_subgroups.common.inc
+++ b/og_subgroups.common.inc
@@ -5,6 +5,8 @@
  * Common functions used in og_subgroups.
  */
 
+define('OG_RECURSION_LIMIT', 50);
+
 /**
  * Get parent groups.
  *
@@ -28,8 +30,12 @@
  * @param $parents
  *   An array used for tracking all parents.
  */
-function og_subgroups_get_parents($entity_type = 'node', $entity, &$paths = array(), $reverse = FALSE, &$parents = array()) {
+function og_subgroups_get_parents($entity_type = 'node', $entity, &$paths = array(), $reverse = FALSE, &$parents = array(), $count = 0) {
   static $forks;
+  if ($count > OG_RECURSION_LIMIT) {
+    watchdog('og_subgroups', "Recursion limit reached in group tree: entity type: %type, id %id already present in tree: %parents ", array('%type' => $group_type, '%id' => $group_id, '%parents' => serialize($parents)), WATCHDOG_CRITICAL);
+    return;
+  }
   $groups = og_get_entity_groups($entity_type, $entity);
   if (!empty($groups)) {
     // We still have parents to record.
@@ -51,7 +57,7 @@ function og_subgroups_get_parents($entity_type = 'node', $entity, &$paths = arra
             'type' => $group_type,
             'id' => $group_id,
           );
-          og_subgroups_get_parents($group_type, $group_id, $paths, $reverse, $parents);
+          og_subgroups_get_parents($group_type, $group_id, $paths, $reverse, $parents, $count + 1);
         }
       }
     }
@@ -214,7 +220,7 @@ function og_subgroups_get_users_group($entity_type = 'node', $entity = NULL, $st
  *  the OG membership ID and the Entity ID as the value. If nothing found,
  *  then an empty array.
  */
-function og_subgroups_get_associated_entities($entity_type, $entity) {
+function og_subgroups_get_associated_entities($entity_type, $entity, $fields = array()) {
   $cache = &drupal_static(__FUNCTION__, array());
   if (is_object($entity)) {
     // Get the entity ID.
@@ -234,6 +240,9 @@ function og_subgroups_get_associated_entities($entity_type, $entity) {
   ->propertyCondition('entity_type', 'user', '!=')
   ->propertyCondition('group_type', $entity_type, '=')
   ->propertyCondition('gid', $id, '=');
+  if ($fields || ($fields = variable_get('og_subgroups_default_fields_' . $entity_type, array()))) {
+    $query->propertyCondition('field_name', $fields, 'IN');
+  }
   $result = $query->execute();
   if (!empty($result['og_membership'])) {
     // Get the group ID from the group membership.
@@ -325,7 +334,7 @@ function _og_subgroups_get_unique_parents($group_type, $group_id, $filter = TRUE
   foreach ($paths as $path) {
     foreach ($path as $group) {
       $entity = $group['entity'];
-      if (!$filter || (isset($entity->{OG_USER_INHERITANCE_FIELD}) && $entity->{OG_USER_INHERITANCE_FIELD}[LANGUAGE_NONE][0]['value'] == 1)) {
+      if (!$filter || (!empty($entity->{OG_USER_INHERITANCE_FIELD}) && $entity->{OG_USER_INHERITANCE_FIELD}[LANGUAGE_NONE][0]['value'] == 1)) {
         $groups[$group['type'] . '-' . $group['id']] = $group;
       }
     }
@@ -374,7 +383,11 @@ function _og_subgroup_get_user_inherited_groups($account) {
  *
  * @see: _og_subgroup_get_user_inherited_groups().
  */
-function _og_subgroups_get_children_groups($group_type, $group_id, &$children_groups) {
+function _og_subgroups_get_children_groups($group_type, $group_id, &$children_groups, $count = 0) {
+  if ($count > OG_RECURSION_LIMIT) {
+    watchdog('og_subgroups', t('Recursion limit, getting children of group %gid', array('%gid' => $group_id)), WATCHDOG_CRITICAL);
+    return;
+  }
   $children = og_subgroups_get_associated_entities($group_type, $group_id);
   foreach ($children as $children_type => $children_ids) {
     $children_entities = entity_load($children_type, $children_ids);
@@ -382,7 +395,7 @@ function _og_subgroups_get_children_groups($group_type, $group_id, &$children_gr
       if (og_is_group($children_type, $child_entity)) {
         $children_groups[$children_type][$child_id] = $child_entity;
         if (isset($child_entity->{OG_USER_INHERITANCE_FIELD}[LANGUAGE_NONE][0]['value']) && $child_entity->{OG_USER_INHERITANCE_FIELD}[LANGUAGE_NONE][0]['value']) {
-          _og_subgroups_get_children_groups($children_type, $child_id, $children_groups);
+          _og_subgroups_get_children_groups($children_type, $child_id, $children_groups, $count++);
         }
       }
     }
