diff --git a/r4032login.install b/r4032login.install
index 2608d40..dca75fe 100644
--- a/r4032login.install
+++ b/r4032login.install
@@ -11,14 +11,19 @@ function r4032login_enable() {
  * Implementation of hook_disable().
  */
 function r4032login_disable() {
-  variable_del('site_403');
+  if (variable_get('site_403','') == 'r4032login') {
+    variable_set('site_403','');
+  }
 }
 
 /**
  * Implementation of hook_uninstall().
  */
 function r4032login_uninstall() {
-  variable_del('r4032login_display_denied_message');
-  variable_del('r4032login_user_register_message');
+  if (variable_get('site_403','') == 'r4032login') {
+    variable_set('site_403','');
+  }
+  variable_del('r4032login_display_anon_denied_message');
+  variable_del('r4032login_display_auth_denied_message');
   variable_del('r4032login_redirect_authenticated_users_to');
 }
diff --git a/r4032login.module b/r4032login.module
index 585b18f..9293a90 100644
--- a/r4032login.module
+++ b/r4032login.module
@@ -36,21 +36,23 @@ function r4032login_access() {
 
 function r4032login_admin_settings() {
   $form = array();
-  $form['r4032login_display_denied_message'] = array(
+  $form['r4032login_display_anon_denied_message'] = array(
     '#type' => 'textarea',
-    '#title' => t('Display access denied message on login page'),
-    '#default_value' => variable_get('r4032login_display_denied_message', 'Access denied. You must login to view this page.')
+    '#title' => t('Display access denied message on login page for annonymous users'),
+    '#default_value' => variable_get('r4032login_display_anon_denied_message',
+                                     'Access denied. You must login to view this page.')
   );
-  $form['r4032login_user_register_message'] = array(
-    '#type' => 'textfield',
-    '#title' => t('User register message'),
-    '#description' => t('The message to display when a logged-in user tries to register another account through the !new_account. Drupal does not allow it, so regular users must log out first. Administrators should create new users in !link.', array('!new_account' => l(t('new account registration form'), 'user/register'), '!link' => l(t('User management'), 'admin/user/user/create'))),
-    '#default_value' => variable_get('r4032login_user_register_message', t('You are not authorized to access this page.'))
+  $form['r4032login_display_auth_denied_message'] = array(
+    '#type' => 'textarea',
+    '#title' => t('Display access denied message on login page for authenticated users'),
+    '#default_value' => variable_get('r4032login_display_auth_denied_message',
+                                     'Access denied. Your account does not have permission to view this page.')
   );
   $form['r4032login_redirect_authenticated_users_to'] = array(
     '#type' => 'textfield',
     '#title' => t('Rediect authenticated users to'),
-    '#description' => t('If an authenticated user tries to access a page they can not, redirect them to the given page. Use &lt;front&gt; for the front page, leave blank for the default access denied page.'),
+    '#description' => t('If an authenticated user tries to access a page they can not, redirect them to the given page.
+                         Use &lt;front&gt; for the front page, leave blank for the default access denied page.'),
     '#default_value' => variable_get('r4032login_redirect_authenticated_users_to', ''),
   );
   return system_settings_form($form);
@@ -64,9 +66,6 @@ function r4032login_theme() {
     'r4032login_denied' => array(
       'arguments' => array()
     ),
-    'r4032login_user_register' => array(
-      'arguments' => array()
-    )
   );
 }
 
@@ -78,60 +77,69 @@ function r4032login_theme() {
  * and a query string parameter appended to the url to return after login.
  */
 function r4032login_redirect() {
-  global $user;
-  $redirect = variable_get('r4032login_redirect_authenticated_users_to', '');
+  // unset destination to prevent recursive redirect
+  unset($_REQUEST['destination']);
+ 
   if (user_is_anonymous()) {
-    if (variable_get('r4032login_display_denied_message', 'Access denied. You must login to view this page.')) {
-      drupal_set_message(variable_get('r4032login_display_denied_message', 'Access denied. You must login to view this page.'), 'error');
-    }
-    // A special case for the homepage.
-    if (empty($_REQUEST['q'])) {
-      $_REQUEST['q'] = '<front>';
-    }
-    // Check for path prefix and strip it out if its found.
-    $path = _r4032login_remove_language(drupal_urlencode($_REQUEST['q']));
-    // using drupal_goto() with destination set causes a recursive redirect loop
-    header('Location: '. url('user/login', array('query' => 'destination='. $path, 'absolute' => TRUE)), TRUE, 302);
-    exit;
-  }
-  elseif (!empty($redirect)) {
-    header('Location: '. url($redirect));
-    exit;
-  }
-  // checking arg() returns r4032login
-  elseif ($_REQUEST['q'] == 'user/register') {
-    print theme('page', theme('r4032login_user_register'));
-    exit;
+    //Anonymous User
+    drupal_set_message(t(variable_get('r4032login_display_denied_message',
+                                      'Access denied. You must login to view this page.')), 'error');
+    drupal_goto('user/login', r4032login_destination());
   }
   else {
-    print theme('page', theme('r4032login_denied'));
-    exit;
+    //Authenticated user
+    $redirect = variable_get('r4032login_redirect_authenticated_users_to', '');
+    if (!empty($redirect)) {
+      drupal_goto($redirect);
+    } else {
+      return theme('r4032login_denied');
+    }
   }
 }
 
 /**
- * Special handling for sites that use localization.
- *
- * @see http://drupal.org/node/339120
+ * like drupal_get_destination(), except not useless because
+ * Drupal has reset $_REQUEST['destination'] and $_GET['q']
  */
-function _r4032login_remove_language($destination) {
-  $prefix = arg(0, $destination);
-  $languages = language_list();
+function r4032login_destination() {
+  // Drupal has reset $_GET[q], so we need a workaround.
+  $internal_path = substr(request_uri(), strlen(base_path()));
+  if (!empty($internal_path)) {
+    if (!variable_get('clean_url', 0)) {
+      // Clean URLs disabled, so break apart the query string and
+      // pull out the path.
+      $internal_path = parse_url($internal_path);
+      $queryarray = explode('&', $internal_path['query']);
+      $path = str_replace('q=', '', $queryarray[0]);
+      unset($queryarray[0]);
+      $query = !empty($queryarray) ? '?' . implode('&', $queryarray) : '';
+      $internal_path = $path . $query;
+    }
+    // If the language path prefixing is enabled remove it from the path.
+    if (variable_get('language_negotiation', FALSE)) {
+      $args = explode('/', $internal_path);
+      $prefix = array_shift($args);
 
-  while (list(, $lang) = each($languages)) {
-    if (strcasecmp($prefix, $lang->language) == 0) {
-      return substr($destination, strlen($prefix) + 1); // remove prefix and slash
+      // Search prefix within enabled languages.
+      $languages = language_list('enabled');
+      foreach ($languages[1] as $language) {
+        if (!empty($language->prefix) && $language->prefix == $prefix) {
+          // Found a match, rebuild the path without the language.
+          $internal_path = implode('/', $args);
+          break;
+        }
+      }
     }
+    return 'destination=' . urlencode($internal_path);
+  }
+  else {
+    //fall back to front page
+    return 'destination=' . urlencode('<front>');
   }
-  return $destination;
 }
 
 function theme_r4032login_denied() {
   drupal_set_title(t('Access denied'));
-  return '<p>'. t('You are not authorized to access this page.') .'</p>';
-}
-
-function theme_r4032login_user_register() {
-  drupal_set_title(t('Access denied'));
-  return '<p>'. variable_get('r4032login_user_register_message', t('You are not authorized to access this page.')) .'</p>';
+  return '<p>'. t(variable_get('r4032login_display_auth_denied_message',
+                               'Access denied. Your account does not have permission to view this page.')) . '</p>';
 }
