diff --git a/linkedin.inc b/linkedin.inc
index 843031d..62bbfae 100644
--- a/linkedin.inc
+++ b/linkedin.inc
@@ -9,15 +9,16 @@
  * Let the user authorize us to gain acces to his profile, and store credentials.
  * Also used for login.
  */
+
 function linkedin_access_token($account) {
-  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();
@@ -28,6 +29,7 @@ function linkedin_access_token($account) {
   $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);
@@ -39,93 +41,184 @@ function linkedin_access_token($account) {
     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_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' => '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_result(db_query("SELECT token_secret FROM {linkedin_token} WHERE uid = %d AND type = 'request'", $account->uid));
-    }
-    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_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");
+    // 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');
     }
-    else {
-      $uid = 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']));
+    //save acces token for future requests
+    $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']));
+    $login_as = user_load($account);
+    user_external_login($login_as);
+    drupal_goto("user/$account->uid/edit/linkedin");
+  }
+}
 
-      if ($uid > 0) {
-        module_invoke_all('linkedin_external_login', $uid);
+// 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');
       }
-      else {
-        module_invoke_all('linkedin_tie_external_login', $uid, $oauth['oauth_token'], $oauth['oauth_token_secret']);
+      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");
   }
 }
 
@@ -136,6 +229,7 @@ function linkedin_access_token($account) {
  * $uid : the uid of the user we want to access infos
  * $fields : the fields we want to retrieve, as an array (see http://developer.linkedin.com/docs/DOC-1061 for complete list). 
  */
+
 function linkedin_get_profile_fields($uid, $fields = array()) {
   $base_url = "https://api.linkedin.com/v1/people/";
 
@@ -159,8 +253,7 @@ function linkedin_get_profile_fields($uid, $fields = array()) {
     $tokens = $row;
     $append = '~';
     $type = 'auth';
-  }
-  else {
+  } else {
     $tokens = db_fetch_array(db_query("SELECT * FROM {linkedin_token} WHERE uid = %d AND type = 'access'", $user->uid));
     if (!$tokens) {
       //We don't have any LinkedIn account associated with the user viewing the profile.
@@ -168,8 +261,7 @@ function linkedin_get_profile_fields($uid, $fields = array()) {
       $tokens = $row;
       $append = '~:public';
       $type = 'public';
-    }
-    else {
+    } else {
       //Fetch profile. Fields returned will depend on the relationships between viewing/viewed users
       $authname = db_result(db_query("SELECT authname FROM {authmap} WHERE uid = %d AND module = 'linkedin'", $uid));
       $append = 'id=' . $authname;
@@ -190,8 +282,7 @@ function linkedin_get_profile_fields($uid, $fields = array()) {
   }
   if (isset($response['person'])) {
     $response = $response['person'];
-  }
-  else {
+  } else {
     $response = $response['error'];
   }
   if (variable_get('linkedin_debug_mode', 0) == 1) {
@@ -208,6 +299,7 @@ function linkedin_get_profile_fields($uid, $fields = array()) {
  * $url : full request url to a linkedin API ressource (see API doc for syntax)
  * $tokens : the user tokens, as an array containing keys 'token_key' and 'token_secret' with their value  
  */
+
 function linkedin_get_fields($url, $tokens) {
   $signature = new OAuthSignatureMethod_HMAC_SHA1();
   $consumer_key = variable_get('linkedin_consumer_key', '');
@@ -237,12 +329,13 @@ function linkedin_get_fields($url, $tokens) {
  * $api : the api we want to use to update. For now, only Share API is available
  * $body : The content to be sent.
  */
+
 function linkedin_put_profile_field($uid, $body, $api = 'shares') {
   $base_url = 'https://api.linkedin.com/v1/people/~/';
   $url = $base_url . $api;
 
   if ($api == 'shares') {
-    $xml  = '<share>';
+    $xml = '<share>';
     if (isset($body['comment'])) {
       $xml .= '<comment>' . $body['comment'] . '</comment>';
     }
@@ -260,8 +353,7 @@ function linkedin_put_profile_field($uid, $body, $api = 'shares') {
     }
     $xml .= '<visibility><code>anyone</code></visibility>';
     $xml .= '</share>';
-  }
-  else {
+  } else {
     // Unsupported update method
     $message = t('Linkedin debug : Unsupported update method "@method"', array('@method' => $api));
     if (variable_get('linkedin_debug_mode', 0) == 1) {
@@ -277,7 +369,7 @@ function linkedin_put_profile_field($uid, $body, $api = 'shares') {
   $consumer = new OAuthConsumer($consumer_key, $consumer_secret, NULL);
   $row = db_fetch_array(db_query("SELECT * FROM {linkedin_token} WHERE uid = %d AND type = 'access'", $uid));
   $token = new OAuthConsumer($row['token_key'], $row['token_secret'], 1);
-  $request = OAuthRequest::from_consumer_and_token($consumer, $token, 'POST', $url, array ());
+  $request = OAuthRequest::from_consumer_and_token($consumer, $token, 'POST', $url, array());
   $request->sign_request($signature, $consumer, $token);
   $header = $request->to_header();
   $response = _linkedin_http_request($url, $header, $xml);
@@ -294,6 +386,7 @@ function linkedin_put_profile_field($uid, $body, $api = 'shares') {
 /*
  * Some internal helper functions...
  */
+
 function _linkedin_http_request($url, $header, $body = NULL) {
 
   $ch = curl_init();
@@ -307,8 +400,7 @@ function _linkedin_http_request($url, $header, $body = NULL) {
     curl_setopt($ch, CURLOPT_POST, 1);
     if ($body == 'token_request') {
       curl_setopt($ch, CURLOPT_POSTFIELDS, '');
-    }
-    else {
+    } else {
       curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
       curl_setopt($ch, CURLOPT_HTTPHEADER, array($header, 'Content-Type: text/xml;charset=utf-8'));
       curl_setopt($ch, CURLOPT_POST, 1);
@@ -324,6 +416,7 @@ function _linkedin_http_request($url, $header, $body = NULL) {
 /*
  * Parser function. Based on xml2array() by Binny V A :  http://www.bin-co.com/php/scripts/xml2array/
  */
+
 function _linkedin_parse_fields($contents) {
   if (!$contents) {
     return array();
@@ -354,12 +447,12 @@ function _linkedin_parse_fields($contents) {
 
   $current = &$xml_array; //Reference
   //Go through the tags.
-  $repeated_tag_index = array();//Multiple tags with same name will be turned into an array
+  $repeated_tag_index = array(); //Multiple tags with same name will be turned into an array
   foreach ($xml_values as $data) {
-    unset($attributes, $value);//Remove existing values, or there will be trouble
+    unset($attributes, $value); //Remove existing values, or there will be trouble
     //This command will extract these variables into the foreach scope
     // tag(string), type(string), level(int), attributes(array).
-    extract($data);//We could use the array by itself, but this cooler.
+    extract($data); //We could use the array by itself, but this cooler.
 
     $result = array();
     $attributes_data = array();
@@ -378,14 +471,12 @@ function _linkedin_parse_fields($contents) {
         $repeated_tag_index[$tag . '_' . $level] = 1;
 
         $current = &$current[$tag];
-      }
-      else { //There was another element with the same tag name
+      } else { //There was another element with the same tag name
         if (isset($current[$tag][0])) {//If there is a 0th element it is already an array
           $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
           $repeated_tag_index[$tag . '_' . $level]++;
-        }
-        else {//This section will make the value an array if multiple tags with the same name appear together
-          $current[$tag] = array($current[$tag], $result);//This will combine the existing item and the new item together to make an array
+        } else {//This section will make the value an array if multiple tags with the same name appear together
+          $current[$tag] = array($current[$tag], $result); //This will combine the existing item and the new item together to make an array
           $repeated_tag_index[$tag . '_' . $level] = 2;
 
           if (isset($current[$tag . '_attr'])) { //The attribute of the last(0th) tag must be moved as well
@@ -396,8 +487,7 @@ function _linkedin_parse_fields($contents) {
         $last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1;
         $current = &$current[$tag][$last_item_index];
       }
-    }
-    elseif ($type == "complete") { //Tags that ends in 1 line '<tag />'
+    } elseif ($type == "complete") { //Tags that ends in 1 line '<tag />'
       //See if the key is already taken.
       if (!isset($current[$tag])) { //New Key
         $current[$tag] = $result;
@@ -405,22 +495,19 @@ function _linkedin_parse_fields($contents) {
         if ($attributes_data) {
           $current[$tag . '_attr'] = $attributes_data;
         }
-      }
-      else { //If taken, put all things inside a list(array)
+      } else { //If taken, put all things inside a list(array)
         if (isset($current[$tag][0]) && is_array($current[$tag])) {//If it is already an array...
           // ...push the new element into that array.
           $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
 
 
           $repeated_tag_index[$tag . '_' . $level]++;
-        }
-        else { //If it is not an array...
+        } else { //If it is not an array...
           $current[$tag] = array($current[$tag], $result); //...Make it an array using using the existing value and the new value
           $repeated_tag_index[$tag . '_' . $level] = 2; //0 and 1 index is already taken
         }
       }
-    }
-    elseif ($type == 'close') { //End of tag '</tag>'
+    } elseif ($type == 'close') { //End of tag '</tag>'
       $current = &$parent[$level - 1];
     }
   }
@@ -431,6 +518,7 @@ function _linkedin_parse_fields($contents) {
 /*
  * Filter the requested fields to match the type of profile (public or auth)
  */
+
 function _linkedin_build_fields_request($fields, $type = 'auth') {
   if (!empty($fields) || is_array($fields)) {
     $fieldslist = _linkedin_list_fields($type);
@@ -449,6 +537,7 @@ function _linkedin_build_fields_request($fields, $type = 'auth') {
  * Provides a list of available LinkedIn fields
  * $type : 'public' or 'auth'. Wether we need 'public' or 'standard' fields
  */
+
 function _linkedin_list_fields($type = 'auth') {
   $fields = array(
       'id',
