diff --git a/linkedin.inc b/linkedin.inc
index dd7e90c..5399cb6 100644
--- a/linkedin.inc
+++ b/linkedin.inc
@@ -15,17 +15,13 @@
  * @see http://drupal.org/node/1354
  */
 function linkedin_access_token($account) {
-  //Get sure library is loaded before doing anything.
-  linkedin_init();
-
-  if ($account->uid < 1) {
-    $anon = TRUE;
-    $back = 'login';
-  }
-  else {
-    $anon = FALSE;
-    $back = 'token';
+  if ($account->uid != 0) {
+    _authenticated_linkedin_access_token($account);
+    return;
   }
+
+  $anon = TRUE;
+  $back = 'login';
   //setting up variables
   $base_url = "https://api.linkedin.com/uas/oauth";
   $signature = new OAuthSignatureMethod_HMAC_SHA1();
@@ -33,9 +29,10 @@ function linkedin_access_token($account) {
   $consumer_secret = variable_get('linkedin_consumer_secret', '');
   $consumer = new OAuthConsumer($consumer_key, $consumer_secret, NULL);
   $random = md5(rand());
-  $callback = url('linkedin/' . $back . '/' . $account->uid, array('absolute' => TRUE, 'query' => array('action' => $random))); // random will be used to discard direct call to the path and for anonymous
-  // First or renewal request
+  $callback = url('linkedin/' . $back . '/' . $account->uid, array('absolute' => TRUE)) . '&action=' . $random; // random will be used to discard direct call to the path and for anonymous
+  // First or renewal request  
   if (!isset($_GET['action']) || $_GET['action'] != $_SESSION['random']) {
+    // STEP 1: Get Request Token...
     $_SESSION['random'] = $random;
     $url = $base_url . "/requestToken";
     $request = OAuthRequest::from_consumer_and_token($consumer, NULL, 'POST', $url);
@@ -44,98 +41,87 @@ function linkedin_access_token($account) {
     $header = $request->to_header();
     $response = _linkedin_http_request($url, $header, 'token_request');
     parse_str($response, $oauth);
-    if (isset($oauth['oauth_problem'])) {
+    if ($oauth['oauth_problem']) {
       if (variable_get('linkedin_debug_mode', 0) == 1) {
         drupal_set_message(t('LinkedIn debug : received error response : @error'), array('@error' => $oauth['oauth_problem']), 'warning');
-      }
-      else {
+      } else {
         drupal_set_message(t('There was a problem with the configuration of LinkedIn on this website. Please try again later.'), 'error');
       }
       watchdog('linkedin', t('Linkedin reported the following response : @error'), array('@error' => $oauth['oauth_problem']), WATCHDOG_ERROR);
       drupal_goto();
     }
     if ($oauth['oauth_token']) {
-
-      if (!$anon) { // Logged in user : store request token for next step in db instead of $_SESSION for security       
-        db_delete('linkedin_token')
-          ->condition('uid', $account->uid)
-          ->execute();
-        $sql = array(
-          'uid' => $account->uid,
-          'token_key' => $oauth['oauth_token'],
-          'token_secret' => $oauth['oauth_token_secret'],
-          'type' => 'request',
-        );
-        drupal_write_record('linkedin_token', $sql);
-        //Redirect the user to the authentication/authorisation page. This will authorise the token in LinkedIn
-        drupal_goto($base_url . '/authorize?oauth_token=' . $oauth['oauth_token']);
-      }
-      else { // Anonymous user. We can't use db storage, but $_SESSION can not lead to identity forgery for anonymous
-        $_SESSION['oauth_token_secret'] = $oauth['oauth_token_secret'];
-        //Redirect the user to the authentication/authorisation page. This will authorise the token in LinkedIn
-        drupal_goto($base_url . '/authenticate?oauth_token=' . $oauth['oauth_token']);
-      }
-    }
-    else {
+      // STEP 2: Forward to Linkedin...
+      $_SESSION['oauth_token_secret'] = $oauth['oauth_token_secret'];
+      $_SESSION['linkedin_oauth_token'] = $oauth['oauth_token'];
+      //Redirect the user to the authentication/authorisation page. This will authorise the token in LinkedIn
+      drupal_goto($base_url . '/authenticate?oauth_token=' . $oauth['oauth_token']);
+    } else {
       if (variable_get('linkedin_debug_mode', 0) == 1) {
         drupal_set_message(t('LinkedIn debug : received no answer from linkedin.com. Check your API credential at admin/settings/linkedin'));
-      }
-      else {
+      } else {
         drupal_set_message(t('There was a problem with the configuration of LinkedIn on this website. Please try again later.'), 'error');
       }
       watchdog('linkedin', t('The website could not communicate with LinkedIn. It is likely your API credentials are misconfigured.'), array(), WATCHDOG_ERROR);
       drupal_goto();
     }
-  }
-  else {
+  } else {
     //this is called when the OAuth callback is invoked. User has authorised the token.
     //Now retrieve the stored request token and ask for an access token
-
-    $url = $base_url . '/accessToken';
-    if (!$anon) {
-      $result = db_query("SELECT token_secret FROM {linkedin_token} WHERE uid = :uid AND type = :type", array(':uid' => $account->uid, ':type' => 'request'))->fetchField();
-    }
-    else {
-      $result = $_SESSION['oauth_token_secret'];
+    // STEP 3: Check whether user has accepted or denied...
+    if (empty($_REQUEST['oauth_token']) || empty($_REQUEST['oauth_verifier']) || $_REQUEST['oauth_token'] != $_SESSION['linkedin_oauth_token']) {
+      drupal_set_message(t('Please accept the access for Linkedin based login.'), 'error');
+      drupal_goto();
     }
+    // STEP 4: Get the access token...
+    $url = $base_url . '/accessToken';
+    $result = $_SESSION['oauth_token_secret'];
     $token = new OAuthConsumer($_REQUEST['oauth_token'], $result, 1);
     $request = OAuthRequest::from_consumer_and_token($consumer, $token, "POST", $url);
     $request->set_parameter("oauth_verifier", $_REQUEST['oauth_verifier']);
     $request->sign_request($signature, $consumer, $token);
     $header = $request->to_header();
-    $response = _linkedin_http_request($url, $header, 'token_request');
+    $response = _linkedin_http_request($url, $header, 'access_request');
     parse_str($response, $oauth);
-    if (!$anon) {
-      //Check if the same LinkedIn account is not already tied to another user
-      $result = db_query("SELECT uid FROM {linkedin_token} WHERE token_key = :token_key AND token_secret = :token_secret AND type = :type ", array(':token_key' => $oauth['oauth_token'], ':token_secret' => $oauth['oauth_token_secret'], ':type' => 'access'))->fetchField();
-      if ($result) {
-        $registered = user_load($result);
-        drupal_set_message(t('Sorry, this LinkedIn account is already associated with user !registered', array('!registered' => l($registered->name, 'user/' . $result))), 'warning');
-        drupal_goto('user/' . $account->uid . '/edit/linkedin');
-      }
-      //save acces token for future requests
-      $sql = array(
+    // Now we have linkedin access... Get the user information and create a user...
+    $base_user_url = "https://api.linkedin.com/v1/people/";
+    $tokens = array('token_key' => $oauth['oauth_token'], 'token_secret' => $oauth['oauth_token_secret']);
+    $append = '~';
+    $type = 'auth';
+    $append .= _linkedin_build_fields_request(array('first-name', 'last-name', 'id'), $type);
+    $url = $base_user_url . $append;
+    $response = linkedin_get_fields($url, $tokens);
+    // Now we have linkedin person information...
+    // Lets create the user and save all the information...
+    user_external_login_register($response['person']['first-name'] . '.' . $response['person']['last-name'], 'linkedin');
+    global $user;
+    $newUser = array(
+        'pass' => 'password', // note: do not md5 the password
+        'mail' => 'email address',
+        'status' => 1
+    );
+    $account = user_save($user, $newUser);
+    // Save the linkedin access key information...
+    //Check if the same LinkedIn account is not already tied to another user
+    $result = db_result(db_query("SELECT uid FROM {linkedin_token} WHERE token_key = '%s' AND token_secret = '%s' AND type = 'access' ", $oauth['oauth_token'], $oauth['oauth_token_secret']));
+    if ($result) {
+      $registered = user_load($result);
+      drupal_set_message(t('Sorry, this LinkedIn account is already associated with user !registered', array('!registered' => l($registered->name, 'user/' . $result))), 'warning');
+      drupal_goto('user/' . $account->uid); // . '/edit/linkedin');
+    }
+    //save acces token for future requests
+    $sql = array(
         'uid' => $account->uid,
         'token_key' => $oauth['oauth_token'],
         'token_secret' => $oauth['oauth_token_secret'],
         'type' => 'access',
-      );
-      drupal_write_record('linkedin_token', $sql, array('uid'));
-      //associate LinkedIn ID with uid for future use
-      $id = linkedin_get_profile_fields($account->uid, array('id'), TRUE);
-      user_set_authmaps($account, array('authmap_linkedin' => $id['id']));
-      drupal_goto("user/$account->uid/edit/linkedin");
-    }
-    else {
-      $uid = db_query("SELECT uid FROM {linkedin_token} WHERE token_key = :token_key AND token_secret = :token_secret AND type = :type ", array(':token_key' => $oauth['oauth_token'], ':token_secret' => $oauth['oauth_token_secret'], ':type' => 'access'))->fetchField();
-
-      if ($uid > 0) {
-        module_invoke_all('linkedin_external_login', $uid);
-      }
-      else {
-        module_invoke_all('linkedin_tie_external_login', $uid, $oauth['oauth_token'], $oauth['oauth_token_secret']);
-      }
-    }
+    );
+    $write_return = drupal_write_record('linkedin_token', $sql);
+    //associate LinkedIn ID with uid for future use
+    user_set_authmaps($account, array('authmap_linkedin' => $response['person']['id']));
+    $login_as = user_load($account);
+    user_external_login($login_as);
+    drupal_goto("user/$account->uid"); ///edit/linkedin");
   }
 }
 
@@ -527,3 +513,103 @@ function _linkedin_list_fields($type = 'auth') {
   }
   return $fields;
 }
+
+// Use only for non-anonymous user....
+function _authenticated_linkedin_access_token($account) {
+  $back = 'login';
+  //setting up variables
+  $base_url = "https://api.linkedin.com/uas/oauth";
+  $signature = new OAuthSignatureMethod_HMAC_SHA1();
+  $consumer_key = variable_get('linkedin_consumer_key', '');
+  $consumer_secret = variable_get('linkedin_consumer_secret', '');
+  $consumer = new OAuthConsumer($consumer_key, $consumer_secret, NULL);
+  $random = md5(rand());
+  $callback = url('linkedin/' . $back . '/' . $account->uid, array('absolute' => TRUE)) . '&action=' . $random; // random will be used to discard direct call to the path and for anonymous
+  // First or renewal request  
+  if (!isset($_GET['action']) || $_GET['action'] != $_SESSION['random']) {
+    // STEP 1: Get Request Token...
+    $_SESSION['random'] = $random;
+    $url = $base_url . "/requestToken";
+    $request = OAuthRequest::from_consumer_and_token($consumer, NULL, 'POST', $url);
+    $request->set_parameter("oauth_callback", $callback);
+    $request->sign_request($signature, $consumer, NULL);
+    $header = $request->to_header();
+    $response = _linkedin_http_request($url, $header, 'token_request');
+    parse_str($response, $oauth);
+    if ($oauth['oauth_problem']) {
+      if (variable_get('linkedin_debug_mode', 0) == 1) {
+        drupal_set_message(t('LinkedIn debug : received error response : @error'), array('@error' => $oauth['oauth_problem']), 'warning');
+      } else {
+        drupal_set_message(t('There was a problem with the configuration of LinkedIn on this website. Please try again later.'), 'error');
+      }
+      watchdog('linkedin', t('Linkedin reported the following response : @error'), array('@error' => $oauth['oauth_problem']), WATCHDOG_ERROR);
+      drupal_goto();
+    }
+    if ($oauth['oauth_token']) {
+      // STEP 2: Forward to Linkedin...
+      $_SESSION['oauth_token_secret'] = $oauth['oauth_token_secret'];
+      $_SESSION['linkedin_oauth_token'] = $oauth['oauth_token'];
+      //Redirect the user to the authentication/authorisation page. This will authorise the token in LinkedIn
+      drupal_goto($base_url . '/authenticate?oauth_token=' . $oauth['oauth_token']);
+    } else {
+      if (variable_get('linkedin_debug_mode', 0) == 1) {
+        drupal_set_message(t('LinkedIn debug : received no answer from linkedin.com. Check your API credential at admin/settings/linkedin'));
+      } else {
+        drupal_set_message(t('There was a problem with the configuration of LinkedIn on this website. Please try again later.'), 'error');
+      }
+      watchdog('linkedin', t('The website could not communicate with LinkedIn. It is likely your API credentials are misconfigured.'), array(), WATCHDOG_ERROR);
+      drupal_goto();
+    }
+  } else {
+    //this is called when the OAuth callback is invoked. User has authorised the token.
+    //Now retrieve the stored request token and ask for an access token
+    // STEP 3: Check whether user has accepted or denied...
+    if (empty($_REQUEST['oauth_token']) || empty($_REQUEST['oauth_verifier']) || $_REQUEST['oauth_token'] != $_SESSION['linkedin_oauth_token']) {
+      drupal_set_message(t('Please accept the access for Linkedin based login.'), 'error');
+      drupal_goto();
+    }
+    // STEP 4: Get the access token...
+    $url = $base_url . '/accessToken';
+    $result = $_SESSION['oauth_token_secret'];
+    $token = new OAuthConsumer($_REQUEST['oauth_token'], $result, 1);
+    $request = OAuthRequest::from_consumer_and_token($consumer, $token, "POST", $url);
+    $request->set_parameter("oauth_verifier", $_REQUEST['oauth_verifier']);
+    $request->sign_request($signature, $consumer, $token);
+    $header = $request->to_header();
+    $response = _linkedin_http_request($url, $header, 'access_request');
+    parse_str($response, $oauth);
+    // Now we have linkedin access... Get the user information and create a user...
+    $base_user_url = "https://api.linkedin.com/v1/people/";
+    $tokens = array('token_key' => $oauth['oauth_token'], 'token_secret' => $oauth['oauth_token_secret']);
+    $append = '~';
+    $type = 'auth';
+    $append .= _linkedin_build_fields_request(array('first-name', 'last-name', 'id'), $type);
+    $url = $base_user_url . $append;
+    $response = linkedin_get_fields($url, $tokens);
+    // Now we have linkedin person information...
+    // Lets create the user and save all the information...
+    //user_external_login_register($response['person']['first-name'] . '.' . $response['person']['last-name'], 'linkedin');
+    global $user;
+    $account = $user;
+    // Save the linkedin access key information...
+    //Check if the same LinkedIn account is not already tied to another user
+    $result = db_result(db_query("SELECT uid FROM {linkedin_token} WHERE token_key = '%s' AND token_secret = '%s' AND type = 'access' ", $oauth['oauth_token'], $oauth['oauth_token_secret']));
+    if ($result) {
+      $registered = user_load($result);
+      drupal_set_message(t('Sorry, this LinkedIn account is already associated with user !registered', array('!registered' => l($registered->name, 'user/' . $result))), 'warning');
+      drupal_goto('user/' . $account->uid);// . '/edit/linkedin');
+    }
+    //save acces token for future requests
+    db_query("DELETE FROM {linkedin_token} WHERE uid = %d", $account->uid);
+    $sql = array(
+        'uid' => $account->uid,
+        'token_key' => $oauth['oauth_token'],
+        'token_secret' => $oauth['oauth_token_secret'],
+        'type' => 'access',
+    );
+    $write_return = drupal_write_record('linkedin_token', $sql);
+    //associate LinkedIn ID with uid for future use
+    user_set_authmaps($account, array('authmap_linkedin' => $response['person']['id']));
+    drupal_goto("user/$account->uid");///edit/linkedin");
+  }
+}
\ No newline at end of file
