diff --git a/workflow.module b/workflow.module
index 8a11f9d..b6ed186 100644
--- a/workflow.module
+++ b/workflow.module
@@ -181,73 +181,94 @@ function workflow_user_delete($account) {
  * Implements hook_tokens().
  */
 function workflow_tokens($type, $tokens, array $data = array(), array $options = array()) {
-  $values = array();
-  switch ($type) {
-    case 'node':
-    case 'workflow':
-      $node = $data['node'];
-      if ($workflow = workflow_get_workflow_type_map_by_type($node->type)) {
-        // TODO If we ever move Workflow to allow M:M content types to workflows, this will need updating.
-        if ($workflow = workflow_get_workflows_by_wid($workflow->wid)) {
-          $wid = $workflow->wid;
-          $values['workflow-name'] = $workflow->name;
-          $last_history = workflow_get_workflow_node_history_by_nid($node->nid, 1);
-          if (isset($node->workflow) && !isset($node->workflow_stamp)) {
-            // The node is being submitted but the form data has not been saved to the database yet,
-            // so we set the token values from the workflow form fields.
-            $sid = $node->workflow;
-            $old_sid = isset($last_history->sid) ? $last_history->sid : workflow_get_creation_state_by_wid($workflow->wid);
-            $sid = workflow_get_workflow_states_by_sid($sid);
-            $old_sid = workflow_get_workflow_states_by_sid($old_sid);
-            $date = REQUEST_TIME;
-            $user_name = $node->uid ? $node->name : variable_get('anonymous', 'Anonymous');
-            $uid = $node->uid;
-            $mail = ($node->uid && isset($node->user_mail)) ? $node->user_mail : '';
-            $comment = isset($node->workflow_comment) ? $node->workflow_comment : '';
-          }
-          elseif (!isset($node->workflow) && empty($last_history->sid)) {
-            // If the state is not specified and the node has no workflow history,
-            // the node is being inserted and will soon be transitioned to the first valid state.
-            // We find this state using the same logic as workflow_nodeapi().
-            $choices = workflow_field_choices($node);
-            $keys = array_keys($choices);
-            $sid = array_shift($keys);
-            $old_sid = workflow_get_creation_state_by_wid($workflow->wid);
-            $sid = workflow_get_workflow_states_by_sid($sid);
-            $old_sid = workflow_get_workflow_states_by_sid($old_sid);
-            $date = REQUEST_TIME;
-            $user_name = $node->uid ? $node->name : variable_get('anonymous', 'Anonymous');
-            $uid = $node->uid;
-            $mail = ($node->uid && isset($node->user_mail)) ? $node->user_mail : '';
-            $comment = isset($node->workflow_comment) ? $node->workflow_comment : '';
-          }
-          else {
-            // Default to the most recent transition data in the workflow history table.
-            $account = user_load($last_history->uid);
-            $sid = $last_history->sid;
-            $old_sid = $last_history->old_sid;
-            $sid = workflow_get_workflow_states_by_sid($sid);
-            $old_sid = workflow_get_workflow_states_by_sid($old_sid);
-            $date = $last_history->stamp;
-            $user_name = $account->uid ? $account->name : variable_get('anonymous', 'Anonymous');
-            $uid = $account->uid;
-            $mail = $account->uid ? $account->mail : '';
-            $comment = $last_history->comment;
+  $replacements = array();
+  if (in_array($type, array('node', 'workflow'))&& !empty($data['node'])) {
+    $node = $data['node'];
+    if ($workflow = workflow_get_workflow_type_map_by_type($node->type)) {
+      // TODO If we ever move Workflow to allow M:M content types to workflows, this will need updating.
+      if ($workflow = workflow_get_workflows_by_wid($workflow->wid)) {
+        $wid = $workflow->wid;
+        $last_history = workflow_get_workflow_node_history_by_nid($node->nid, 1);
+        if (isset($node->workflow) && !isset($node->workflow_stamp)) {
+          // The node is being submitted but the form data has not been saved to the database yet,
+          // so we set the token values from the workflow form fields.
+          $sid = $node->workflow;
+          $old_sid = isset($last_history->old_sid) ? $last_history->old_sid : workflow_get_creation_state_by_wid($workflow->wid);
+          $sid = workflow_get_workflow_states_by_sid($sid);
+          $old_sid = workflow_get_workflow_states_by_sid($old_sid);
+          $date = REQUEST_TIME;
+          $user_name = $node->uid ? $node->name : variable_get('anonymous', 'Anonymous');
+          $uid = $node->uid;
+          $mail = ($node->uid && isset($node->user_mail)) ? $node->user_mail : '';
+          $comment = isset($node->workflow_comment) ? $node->workflow_comment : '';
+        }
+        elseif (!isset($node->workflow) && empty($last_history->sid)) {
+          // If the state is not specified and the node has no workflow history,
+          // the node is being inserted and will soon be transitioned to the first valid state.
+          // We find this state using the same logic as workflow_nodeapi().
+          $choices = workflow_field_choices($node);
+          $keys = array_keys($choices);
+          $sid = array_shift($keys);
+          $old_sid = workflow_get_creation_state_by_wid($workflow->wid);
+          $sid = workflow_get_workflow_states_by_sid($sid);
+          $old_sid = workflow_get_workflow_states_by_sid($old_sid);
+          $date = REQUEST_TIME;
+          $user_name = $node->uid ? $node->name : variable_get('anonymous', 'Anonymous');
+          $uid = $node->uid;
+          $mail = ($node->uid && isset($node->user_mail)) ? $node->user_mail : '';
+          $comment = isset($node->workflow_comment) ? $node->workflow_comment : '';
+        }
+        else {
+          // Default to the most recent transition data in the workflow history table.
+          $account = user_load($last_history->uid);
+          $sid = $last_history->sid;
+          $old_sid = $last_history->old_sid;
+          $sid = workflow_get_workflow_states_by_sid($sid);
+          $old_sid = workflow_get_workflow_states_by_sid($old_sid);
+          $date = $last_history->stamp;
+          $user_name = $account->uid ? $account->name : variable_get('anonymous', 'Anonymous');
+          $uid = $account->uid;
+          $mail = $account->uid ? $account->mail : '';
+          $comment = $last_history->comment;
+        }
+        foreach ($tokens as $name => $original) {
+          switch ($name) {
+            case 'workflow-name':
+              $replacements[$original] = $workflow->name;
+              break;
+            case 'workflow-current-state-name':
+              $replacements[$original] = $sid->state;
+              break;
+            case 'workflow-old-state-name':
+              $replacements[$original] = $old_sid->state;
+              break;
+            case 'workflow-current-state-date-iso':
+              $replacements[$original] = date('Ymdhis', $date);
+              break;
+            case 'workflow-current-state-date-tstamp':
+              $replacements[$original] = $date;
+              break;
+            case 'workflow-current-state-date-formatted':
+              $replacements[$original] = date('M d, Y h:i:s', $date);
+              break;
+            case 'workflow-current-state-updating-user-name':
+              $replacements[$original] = check_plain($user_name);
+              break;
+            case 'workflow-current-state-updating-user-uid':
+              $replacements[$original] = $uid;
+              break;
+            case 'workflow-current-state-updating-user-mail':
+              $replacements[$original] = check_plain($mail);
+              break;
+            case 'workflow-current-state-log-entry':
+              $replacements[$original] = filter_xss($comment, array('a', 'em', 'strong'));
+              break;
           }
-          $values['workflow-current-state-name']                =  $sid->state;
-          $values['workflow-old-state-name']                    =  $old_sid->state;
-          $values['workflow-current-state-date-iso']            =  date('Ymdhis', $date);
-          $values['workflow-current-state-date-tstamp']         =  $date;
-          $values['workflow-current-state-date-formatted']      =  date('M d, Y h:i:s', $date);
-          $values['workflow-current-state-updating-user-name']  = check_plain($user_name);
-          $values['workflow-current-state-updating-user-uid']   = $uid;
-          $values['workflow-current-state-updating-user-mail']  = check_plain($mail);
-          $values['workflow-current-state-log-entry']           = filter_xss($comment, array('a', 'em', 'strong'));
         }
       }
-    break;
+    }
   }
-  return $values;
+  return $replacements;
 }
 
 /**
