diff --git a/masquerade.module b/masquerade.module
index 9e8b2f3..9e8a86d 100644
--- a/masquerade.module
+++ b/masquerade.module
@@ -59,7 +59,7 @@ function masquerade_menu() {
     $items['masquerade/switch/' . $default_test_user->uid] = array(
       'title' => 'Masquerade as @testuser',
       'title arguments' => array('@testuser' => $default_test_user->name),
-      'page callback' => 'masquerade_switch_user',
+      'page callback' => 'masquerade_switch_user_page',
       'page arguments' => array(2),
       'access callback' => 'masquerade_access',
       'access arguments' => array('switch'),
@@ -69,7 +69,7 @@ function masquerade_menu() {
 
   $items['masquerade/switch/%'] = array(
     'title' => 'Masquerading',
-    'page callback' => 'masquerade_switch_user',
+    'page callback' => 'masquerade_switch_user_page',
     'page arguments' => array(2),
     'access callback' => 'masquerade_access',
     'access arguments' => array('switch'),
@@ -77,7 +77,7 @@ function masquerade_menu() {
   );
   $items['masquerade/unswitch'] = array(
     'title' => 'Switch back',
-    'page callback' => 'masquerade_switch_back',
+    'page callback' => 'masquerade_switch_back_page',
     'access callback' => 'masquerade_access',
     'access arguments' => array('unswitch'),
     'type' => MENU_NORMAL_ITEM,
@@ -494,7 +494,12 @@ function masquerade_block_1_submit($form, &$form_state) {
   unset($form);
   $uid = db_query('SELECT uid FROM {users} WHERE name=:name', array(':name' => $form_state['values']['masquerade_user_field']))->fetchField();
 
-  masquerade_switch_user($uid);
+  if (!masquerade_switch_user($uid)) {
+    drupal_access_denied();
+  }
+  else {
+    drupal_goto(referer_uri());
+  }
 }
 
 /**
@@ -550,8 +555,26 @@ function masquerade_autocomplete_user($string) {
 }
 
 /**
- * Page callback that allows a user with the right permissions to become
+ * Page callback to switch users.
+ */
+function masquerade_switch_user_page($uid) {
+  if (!masquerade_switch_user($uid)) {
+    drupal_access_denied();
+  }
+  else {
+    drupal_goto(refer_uri());
+  }
+}
+
+/**
+ * Function that allows a user with the right permissions to become
  * the selected user.
+ *
+ * @param $uid
+ *   The user ID to switch to.
+ *
+ * @return
+ *   TRUE if the user was sucessfully switched, or FALSE if there was an error.
  */
 function masquerade_switch_user($uid) {
   if (!is_numeric($uid)) {
@@ -566,18 +589,20 @@ function masquerade_switch_user($uid) {
     'masquerade as user';
   // check to see if we need admin permission
   if (!user_access($perm) && !$GLOBALS['masquerading']) {
-    return drupal_access_denied();
+    watchdog('masquerade', 'This user requires administrative permissions to switch to the user %user.', array('%user' => $new_user->name), WATCHDOG_ERROR);
+    return FALSE;
   }
 
   global $user;
 
   if ($user->uid == $uid || isset($user->masquerading)) {
-    return drupal_access_denied();
+    watchdog('masquerade', 'This user is all ready %user.', array('%user' => $new_user->name), WATCHDOG_ERROR);
+    return FALSE;
   }
 
   if (variable_get('site_offline', 0) && !user_access('administer site configuration', $new_user)) {
-    drupal_set_message(t('The user is not allowed to access site in off-line mode!'), 'error');
-    return drupal_access_denied();
+    drupal_set_message(t('This user is not allowed to access the site while the site is in off-line mode. Please <a href="@site-maintenance">set the site status</a> to "online" to switch to this user.', array('@site-maintenance' => url('admin/settings/site-maintenance'))), 'error');
+    return FALSE;
   }
 
   db_insert('masquerade')
@@ -594,13 +619,24 @@ function masquerade_switch_user($uid) {
   $user->masquerading = $new_user->uid;
   $user = $new_user;
   drupal_save_session(TRUE);
-  drupal_goto($_SERVER['HTTP_REFERER']);
+  return TRUE;
 }
 
 /**
  * Page callback that allows a user who is currently masquerading to revert to their
  * original user session.
  */
+function masquerade_switch_back_page() {
+  global $user;
+  $oldname = $user->name;
+  masquerade_switch_back();
+  drupal_set_message(t('You are no longer masquerading as %masq_as and are now logged in as %user.', array('%user' => $user->name, '%masq_as' => $oldname)));
+  drupal_goto(referer_uri());
+}
+
+/**
+ * Function for a masquerading user to switch back to the previous user.
+ */
 function masquerade_switch_back() {
   // switch user
   global $user;
@@ -619,6 +655,5 @@ function masquerade_switch_back() {
   $user = user_load($uid);
   drupal_save_session(TRUE);
   watchdog('masquerade', 'User %user no longer masquerading as %masq_as.', array('%user' => $user->name, '%masq_as' => $oldname), WATCHDOG_INFO);
-  drupal_set_message(t('No longer masquerading as %masq_as.', array('%masq_as' => $oldname)));
-  drupal_goto($_SERVER['HTTP_REFERER']);
 }
+
