diff --git a/linkedin.inc b/linkedin.inc
index f22ae05..2f95cba 100644
--- a/linkedin.inc
+++ b/linkedin.inc
@@ -37,7 +37,7 @@ function linkedin_access_token($account) {
   // First or renewal request
   if (!isset($_GET['action']) || $_GET['action'] != $_SESSION['random']) {
     $_SESSION['random'] = $random;
-    $url = $base_url . "/requestToken";
+    $url = $base_url . "/requestToken?scope=r_fullprofile+r_emailaddress+r_contactinfo";
     $request = OAuthRequest::from_consumer_and_token($consumer, NULL, 'POST', $url);
     $request->set_parameter("oauth_callback", $callback);
     $request->sign_request($signature, $consumer, NULL);
@@ -52,7 +52,7 @@ function linkedin_access_token($account) {
         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();
+      return drupal_goto();
     }
     if ($oauth['oauth_token']) {
 
@@ -84,13 +84,15 @@ function linkedin_access_token($account) {
         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();
+      return drupal_goto();
     }
   }
   else {
+    if (!isset($_SESSION['oauth_token_secret']) || isset($_GET['oauth_problem'])) {
+      return drupal_goto();
+    }
     //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();
@@ -127,18 +129,115 @@ function linkedin_access_token($account) {
       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']);
+        // @TODO: make creating user an option
+        // We have LinkedIn access, get user email and profile to create 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';
+        
+        // @TODO: select fields in admin, map to drupal fields
+        $append .= _linkedin_build_fields_request(array('first-name', 'last-name', 'id', 
+          'picture-url'), $type);
+        $url = $base_user_url . $append;
+        $response = linkedin_get_fields($url, $tokens);
+        
+        // Get email address
+        $url = $base_user_url .'~/email-address';
+        $email_response = linkedin_get_fields($url, $tokens);
+  
+        if (!isset($email_response['email-address'])) {
+          return drupal_set_message(t('The website could not communicate with LinkedIn. It is likely your API credentials are misconfigured.'), 'error');
+        }
+
+        global $user;
+        $username = $response['person']['first-name'] . ' ' . $response['person']['last-name'];
+
+        $exists = db_select('users', 'u')
+          ->fields('u', array('name', 'mail'))
+          ->condition('name', $username, '=')
+          ->condition('mail', $email_response['email-address'], '=')
+          ->execute()
+          ->fetchObject();
+
+        if ($exists) {
+          if ($exists->name == $username && $exists->mail == $email_response['email-address']) {
+            return drupal_set_message(
+              t('Account with that name and email already exists. You can either !reset your password or !login with your password.', 
+                array(
+                  '!reset' => l(t('reset'), 'user/password'),
+                  '!login' => l(t('login'), 'user/login')
+                )), 'error'
+            );
+          }
+          // Generate random username if name already exists
+          if ($exists->name == $username) {
+            $username = _linkedin_generate_username($username);
+          }
+
+          if ($exists->mail == $email_response['email-address']) {
+            return drupal_set_message(t('Account already exists for your email address.'));
+          }
+        }
+        $newUser = array(
+          'name' => $username,
+          'pass' => user_password(),
+          'mail' =>  $email_response['email-address'],
+          'status' => 1,
+        );
+        $account = user_save($user, $newUser);
+
+        // 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);
+
+        // Associate LinkedIn ID with uid for future use
+        user_set_authmaps($account, array('authmap_linkedin' => $response['person']['id']));
+
+        //user_external_login($account);
+        
+        $form_state['uid'] = $account->uid;
+        user_login_submit(array(), $form_state);        
+        
+        $user = $account;
+        $liuser = $response + $email_response;
+
+        // Allow other modules to manipulate the user information after save.
+        module_invoke_all('linkedin_user_save', $account, $liuser);
+        drupal_goto();
+
       }
     }
   }
 }
 
+function _linkedin_generate_username($username) {
+  do {
+    $username = $username . rand(0, 100);
+    $name = db_select('users', 'u')
+      ->fields('u', array('name'))
+      ->condition('name', $username, '=')
+      ->execute()
+      ->fetchObject();
+
+    if (!$name->name) {
+      return $username;
+    }  
+  } while (TRUE);
+}
+
 /*
  * Let us retrieve profile fields.
  * Returns an array contening the fields requested.
