diff --git a/node_class.module b/node_class.module
index c2b127b..9c89bb9 100644
--- a/node_class.module
+++ b/node_class.module
@@ -17,18 +17,11 @@ function node_class_attributes($node) {
   if ($ret !== FALSE) {
     return $ret;
   }
-  // Creating NULL object 
-  $undef = (object) NULL;
-  $undef->css_class = '';
-  $undef->nid = 0;
-  return $undef;
 }
 
 /*
  * Implementation of hook_form_alter()
  */
-
-
 function node_class_form_alter(&$form, &$form_state, $form_id) {
 	// Check form type && if user_access is ok
   if (isset($form['#node']) && ($form_id == $form['#node']->type .'_node_form') && user_access('administer nodes')) {
@@ -40,11 +33,16 @@ function node_class_form_alter(&$form, &$form_state, $form_id) {
       '#tree' => TRUE,
     );
     
-    // Getting css attributes information
-    $attributes = node_class_attributes($form['#node']);
+    // Getting css attributes information for nodes that already exist.
+    if (isset($form['#node']->nid)) {
+        $attributes = node_class_attributes($form['#node']);
+    }
     
     // Check CSS exists in DB to prepare SQL update
-    if ($attributes->nid) {
+    $old_classes = NULL;
+    // added check up front to prevent object errors. no need for null object
+    if ($attributes && $attributes->nid) {
+      $old_classes = $attributes->css_class;
       $form['node_class']['existing_css'] = array(
         '#type' => 'hidden',
         '#value' => '1',
@@ -54,7 +52,7 @@ function node_class_form_alter(&$form, &$form_state, $form_id) {
     $form['node_class']['css_class'] = array(
       '#type' => 'textfield',
       '#title' => t('CSS class(es)'),
-      '#default_value' => $attributes->css_class,
+      '#default_value' => $old_classes,
       '#description' => t('Separate classes with a space. IMPORTANT: You must add &lt;?php print node_class($node); ?&gt; to your theme\'s node.tpl.php file to make the classes appear.'),
     );
     // Adding submit handler for node_class module
@@ -71,6 +69,14 @@ function node_class_nodeapi(&$node, $op, $a3 = NUL, $a4 = NULL) {
       // Deleting class information for the node
       db_query("DELETE FROM {node_class} WHERE nid='%s'", $node->nid);
       break;
+    case 'insert':
+      // Inserting class information for the node when a class has been entered.
+
+      if (strlen($node->node_class['css_class']) >= 1) {
+        db_query("INSERT INTO {node_class} (nid, css_class) VALUES('%s','%s')", $node->nid, $node->node_class['css_class']);
+      }
+
+      break;
   }
 }
 
@@ -85,12 +91,7 @@ function node_class_nodeapi(&$node, $op, $a3 = NUL, $a4 = NULL) {
       $class = check_plain($form_state['values']['node_class']['css_class']);
       
       // CRUD test
-      if (!isset($form_state['values']['node_class']['existing_css']))  {
-        if (!empty($class)) {
-          db_query("INSERT INTO {node_class} (nid,css_class) VALUES('%s','%s')", $nid, $class);
-        }
-      }
-      else {
+      if (isset($form_state['values']['node_class']['existing_css']))  {
         db_query("UPDATE {node_class} SET css_class='%s' WHERE nid='%s'", $class, $nid);
        }
      }
