diff --git a/masquerade.module b/masquerade.module
index 4367c05..727e3c2 100644
--- a/masquerade.module
+++ b/masquerade.module
@@ -28,25 +28,22 @@ function masquerade_perm() {
 }
 
 /**
- * Implementation of hook_init().
+ * Returns UID the current user was originall, before masquarading.
+ *
+ * @param $reset
+ *   Reset the internal cache.
+ *
+ * @return
+ *   A numeric UID (including 0) else FALSE.
  */
-function masquerade_init() {
+function masquerade_uid($reset = FALSE) {
   global $user;
-
-  // Try to load masqing uid from masquerade table.
-  $uid = db_result(db_query("SELECT uid_from FROM {masquerade} WHERE sid = '%s' AND uid_as = %d", session_id(), $user->uid));
-
-  // We are using identical operator (===) instead of equal (==) because if
-  // $uid === 0 we want to store the session variable. If there's no record in
-  // masquerade table we clear the session variable.
-  if ($uid === FALSE) {
-    if (isset($_SESSION)) {
-      unset($_SESSION['masquerading']);
-    }
-  }
-  else {
-    $_SESSION['masquerading'] = $uid;
+  static $uid;
+  if (!isset($uid) || $reset) {
+    // Try to load masqing uid from masquerade table.
+    $uid = db_result(db_query("SELECT uid_from FROM {masquerade} WHERE sid = '%s' AND uid_as = %d", session_id(), $user->uid));
   }
+  return $uid;
 }
 
 /**
@@ -178,9 +175,9 @@ function masquerade_translated_menu_link_alter(&$item, $map) {
 function masquerade_access($type, $uid = NULL) {
   switch ($type) {
     case 'unswitch':
-      return isset($_SESSION['masquerading']) || arg(2) == 'menu-customize' || arg(2) == 'menu';
+      return masquerade_uid() !== FALSE || arg(2) == 'menu-customize' || arg(2) == 'menu';
     case 'autocomplete':
-      return isset($_SESSION['masquerading']) || (user_access('masquerade as user') || user_access('masquerade as admin'));
+      return masquerade_uid() !== FALSE|| (user_access('masquerade as user') || user_access('masquerade as admin'));
       break;
     case 'user':
       global $user;
@@ -197,7 +194,7 @@ function masquerade_access($type, $uid = NULL) {
           $switch_to_account = db_result(db_query("SELECT TRUE FROM {masquerade_users} WHERE uid_from = %d AND uid_to = %d", $user->uid, $account->uid));
         }
       }
-      return !isset($_SESSION['masquerading']) && (user_access('masquerade as user') || user_access('masquerade as admin') || $switch_to_account);
+      return !masquerade_uid() !== FALSE&& (user_access('masquerade as user') || user_access('masquerade as admin') || $switch_to_account);
       break;
   }
 }
@@ -439,7 +436,7 @@ function masquerade_block($op = 'list', $delta = 0, $edit = array()) {
 function masquerade_block_1($record) {
   global $user;
   $markup_value = '';
-  if (isset($_SESSION['masquerading'])) {
+  if (masquerade_uid() !== FALSE) {
     $quick_switch_link[] = l(t('Switch back'), 'masquerade/unswitch', array('query' => array('token' => drupal_get_token('masquerade/unswitch'))));
     if ($user->uid > 0) {
       $markup_value = t('You are masquerading as <a href="@user-url">%masq_as</a>.', array('@user-url' => url('user/' . $user->uid), '%masq_as' => $user->name)) . theme('item_list', $quick_switch_link);
@@ -513,7 +510,7 @@ function masquerade_block_1_validate($form, &$form_state) {
   if ($name == variable_get('anonymous', t('Anonymous'))) {
     $name = '';
   }
-  if (isset($_SESSION['masquerading'])) {
+  if (masquerade_uid() !== FALSE) {
     form_set_error('masquerade_user_field', t('You are already masquerading. Please <a href="@unswitch">switch back</a> to your account to masquerade as another user.', array('@unswitch' => url('masquerade/unswitch', array('query' => array('token' => drupal_get_token('masquerade/unswitch')))))));
   }
   if (!empty($name) && module_exists('alt_login')) {
@@ -660,7 +657,7 @@ function masquerade_switch_user($uid) {
     'masquerade as user';
 
   // Check to see if we need admin permission.
-  if (!user_access($perm) && !isset($_SESSION['masquerading']) && !db_result(db_query("SELECT TRUE FROM {masquerade_users} WHERE uid_from = %d AND uid_to = %d", $user->uid, $new_user->uid))) {
+  if (!user_access($perm) && !masquerade_uid() !== FALSE&& !db_result(db_query("SELECT TRUE FROM {masquerade_users} WHERE uid_from = %d AND uid_to = %d", $user->uid, $new_user->uid))) {
     watchdog('masquerade', 'This user requires administrative permissions to switch to the user %user.', array('%user' => $new_user->name), WATCHDOG_ERROR);
     return FALSE;
   }
