diff --git a/nodeaccess.module b/nodeaccess.module
index e760b39..2bbfd4d 100644
--- a/nodeaccess.module
+++ b/nodeaccess.module
@@ -85,7 +85,7 @@ function nodeaccess_access($op, $node, $account = NULL) {
   }
   $allowed_types = variable_get('nodeaccess-types', array());
   if ($op == 'grant') {
-    if ($node->nid && $allowed_types[$node->type] &&
+    if (!empty($node->nid) && !empty($allowed_types[$node->type]) &&
         (user_access('grant node permissions', $account) ||
          (user_access('grant editable node permissions', $account) && node_access('update', $node, $account)) ||
          (user_access('grant deletable node permissions', $account) && node_access('delete', $node, $account)) ||
@@ -180,13 +180,13 @@ function nodeaccess_admin_form(&$form_state) {
 
     $form['nodeaccess'][$type]['show'] = array('#type' => 'checkbox',
                                                '#title' => t('Show grant tab for this node type'),
-                                               '#default_value' => $show[$type]);
+                                               '#default_value' => (!empty($show[$type])));
 
     // Set default author permissions for node type.
     $author_prefs = variable_get('nodeaccess_authors', array());
-    $form['nodeaccess'][$type]['author']['grant_view'] = array('#type' => 'checkbox', '#default_value' => $author_prefs[$type]['grant_view']);
-    $form['nodeaccess'][$type]['author']['grant_update'] = array('#type' => 'checkbox', '#default_value' => $author_prefs[$type]['grant_update']);
-    $form['nodeaccess'][$type]['author']['grant_delete'] = array('#type' => 'checkbox', '#default_value' => $author_prefs[$type]['grant_delete']);
+    $form['nodeaccess'][$type]['author']['grant_view'] = array('#type' => 'checkbox', '#default_value' => (!empty($author_prefs[$type]['grant_view'])));
+    $form['nodeaccess'][$type]['author']['grant_update'] = array('#type' => 'checkbox', '#default_value' => (!empty($author_prefs[$type]['grant_update'])));
+    $form['nodeaccess'][$type]['author']['grant_delete'] = array('#type' => 'checkbox', '#default_value' => (!empty($author_prefs[$type]['grant_delete'])));
 
     $perms = variable_get('nodeaccess_' . $type, array());
     foreach ($perms as $perm) {
@@ -304,7 +304,7 @@ function theme_nodeaccess_admin_form_roles($form) {
     $row[] = drupal_render($form[$role]['weight']);
     $rows[] = $row;
   }
-  $output .= theme('table', $header, $rows);
+  $output = theme('table', $header, $rows);
   return $output;
 }
 
@@ -401,8 +401,8 @@ function nodeaccess_grants_form(&$form_state, $node) {
       }
     }
   }
-  $roles = $form_values['rid'];
-  $users = $form_values['uid'];
+  $roles = empty($form_values['rid']) ? NULL : $form_values['rid'];
+  $users = empty($form_values['uid']) ? NULL : $form_values['uid'];
 
   $form['nid'] = array('#type' => 'value', '#value' => $node->nid);
 
@@ -467,11 +467,9 @@ function nodeaccess_grants_form(&$form_state, $node) {
   }
 
   // Autocomplete returns errors if users don't have access to profiles.
+  $form['keys'] = array('#type' => 'textfield', '#size' => 40, '#default_value' => (isset($form_values['keys']) ? $form_values['keys'] : ''));
   if (user_access('access user profiles')) {
-    $form['keys'] = array('#type' => 'textfield', '#default_value' => $form_values['keys'], '#size' => 40, '#autocomplete_path' => 'user/autocomplete');
-  }
-  else {
-    $form['keys'] = array('#type' => 'textfield', '#default_value' => $form_values['keys'], '#size' => 40);
+    $form['keys']['#autocomplete_path'] = 'user/autocomplete';
   }
 
   $form['search'] = array('#type' => 'submit', '#value' => t('Search'));
@@ -558,6 +556,8 @@ function theme_nodeaccess_grants_form($form) {
   $allowed_roles = variable_get('nodeaccess-roles', array());
   $allowed_grants = variable_get('nodeaccess-grants', array());
 
+  $output = '';
+
   // Roles table.
   $roles = element_children($form['rid']);
   if (count($roles) && count($allowed_roles)) {
@@ -601,7 +601,7 @@ function theme_nodeaccess_grants_form($form) {
 
   // Users table.
   unset($rows);
-  $users = element_children($form['uid']);
+  $users = isset($form['uid']) ? element_children($form['uid']) : array();
   if (count($users) > 0) {
     $header = array();
     $header[] = t('User');
@@ -859,14 +859,14 @@ function nodeaccess_insert_userreference($node) {
     foreach ($node->$field_name as $values) {
       // Add the settings for this UID. If the UID is zero, then the
       // userreference field is set to None.
-      if ($values['uid'] > 0) {
+      if (!empty($values['uid'])) {
         $user = user_load($values['uid']);
         $form_values['uid'][$user->uid] = array('name' => $user->name, 'keep' => 1, 'grant_view' => $field['grant_view'], 'grant_update' => $field['grant_update'], 'grant_delete' => $field['grant_delete']);
       }
     }
   }
   // Only do the changes if there are users to save.
-  if (count($form_values['uid']) > 0) {
+  if (!empty($form_values['uid'])) {
     $form_values['nid'] = $node->nid;
     $form_state = array('values' => $form_values);
     _nodeaccess_grants_form_submit(NULL, $form_state);
@@ -889,8 +889,10 @@ function nodeaccess_delete_userreference($node) {
     if (isset($node->$field_name)) {
       foreach ($node->$field_name as $values) {
         // Add the settings for this UID
-        $user = user_load($values['uid']);
-        unset($form_values['uid'][$user->uid]);
+        if (isset($values['uid'])) {
+          $user = user_load($values['uid']);
+          unset($form_values['uid'][$user->uid]);
+        }
       }
     }
   }
