diff --git a/fbconnect.info b/fbconnect.info
index 81e3e94..390cb34 100644
--- a/fbconnect.info
+++ b/fbconnect.info
@@ -1,6 +1,6 @@
-name = Fbconnect
+name = FBConnect
 description = Allows users to connect with Facebook account
-package = "Authentication"
+package = "Facebook Social"
 core = 7.x
 files[] = tests/fbconnect.test
 configure =  admin/config/people/fbconnect
diff --git a/fbconnect.install b/fbconnect.install
index a0b91e2..b1704e0 100644
--- a/fbconnect.install
+++ b/fbconnect.install
@@ -60,18 +60,9 @@ function fbconnect_requirements($phase) {
 }
 
 /**
- * @file
- * Implements hook_install().
- */
-function fbconnect_install() {
-  _fbconnect_change_user_mail_field();
-}
-
-/**
  * Implements hook_uninstall().
  */
 function fbconnect_uninstall() {
-  _fbconnect_change_user_mail_field('uninstall');
   // Delete our module's variable from the variables table.
   variable_del('fbconnect_appid');
   variable_del('fbconnect_base_domaine');
@@ -100,54 +91,9 @@ function fbconnect_uninstall() {
 }
 
 /**
- * Implements hook_schema().
+ * Implements hook_update_N().
+ * Moved fbconnect_users schema to fbconnect_login module
  */
-function fbconnect_schema() {
-  $schema['fbconnect_users'] = array(
-    'fields' => array(
-      'uid' => array(
-        'type' => 'int',
-        'unsigned' => TRUE,
-        'not null' => TRUE,
-        'default' => 0,
-      ),
-      'fbuid' => array(
-        'type' => 'int',
-        'unsigned' => TRUE,
-        'not null' => TRUE,
-        'default' => 0,
-        'size' => 'big',
-      ),
-      'timestamp' => array(
-        'type' => 'int',
-        'unsigned' => TRUE,
-        'not null' => TRUE,
-        'default' => 0,
-      ),
-    ),
-    'primary key' => array('fbuid'),
-  );
-
-  return $schema;
-}
-
-/**
- * Implements hook_schema_alter().
- * @param array $schema
- * @todo remove it / create updating tool
- */
-function fbconnect_schema_alter(&$schema) {
-  $schema['users']['fields']['mail']['length'] = 320;
-}
-
-/**
- * Extend maximum email length to 320 chars
- */
-function _fbconnect_change_user_mail_field($action = 'install') {
-  $schema = array('users' => drupal_get_schema('users'));
-  if ($action == 'install') {
-    fbconnect_schema_alter($schema);
-  }
-  db_change_field('users', 'mail', 'mail', $schema['users']['fields']['mail']);
-  //return $res;
+function fbconnect_update_7001() {
+  drupal_set_installed_schema_version('fbconnect_login', 7000);
 }
diff --git a/fbconnect.module b/fbconnect.module
index 953a02c..91a1aed 100644
--- a/fbconnect.module
+++ b/fbconnect.module
@@ -46,336 +46,10 @@ function fbconnect_menu() {
     'weight' => 3,
   );
 
-  $items['fbconnect/register/prompt'] = array(
-    'title' => 'Facebook connect',
-    'page callback' => 'fbconnect_prompt_page',
-    'type' => MENU_NORMAL_ITEM,
-    'file' => 'fbconnect.pages.inc',
-  );
-
-  $items['fbconnect/register/create'] = array(
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('fbconnect_register_page'),
-    'access arguments' => TRUE,
-    'access callback' => TRUE,
-    'type' => MENU_CALLBACK,
-    'file' => 'fbconnect.pages.inc',
-  );
-
-  $items['fbconnect/login/create'] = array(
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('fbconnect_login_page'),
-    'type' => MENU_CALLBACK,
-    'file' => 'fbconnect.pages.inc',
-  );
-
-  $items['fbconnect/post-remove'] = array(
-    'page callback' => 'fbconnect_post_remove_callback',
-    'access callback' => 'user_access',
-    'access arguments' => array('access content'),
-    'type' => MENU_CALLBACK,
-  );
-
-  $items['fbconnect/link'] = array(
-    'page callback' => 'fbconnect_link_callback',
-    'access callback' => 'user_is_logged_in',
-    'type' => MENU_CALLBACK,
-    'file' => 'fbconnect.pages.inc',
-  );
-
   return $items;
 }
 
 /**
- * Implements hook_block().
- */
-function fbconnect_block_info() {
-  // This example comes from node.module.
-  $blocks['fbconnect_login'] = array(
-    'info' => t('Facebook Login'),
-    'cache' => DRUPAL_NO_CACHE,
-  );
-  return $blocks;
-}
-
-/**
- * Implements hook_block_view().
- */
-function fbconnect_block_view($delta = '') {
-  global $user;
-  $block = array();
-
-  switch ($delta) {
-    case 'fbconnect_login':
-      if ($user->uid) {
-        return;
-      }
-      $block['subject'] = t('Facebook Login');
-      $block['content'] = fbconnect_render_button();
-    break;
-  }
-  return $block;
-}
-
-/**
- * Implements hook_theme().
- */
-function fbconnect_theme() {
-  return array(
-    'fbconnect_user_picture_override' => array(
-      'variables' => array(
-        'fbuid' => NULL,
-        'account' => NULL,
-        'user_url' => NULL,
-        'size' => '',
-      ),
-      'file' => 'fbconnect.pages.inc',
-    ),
-    'fbconnect_login_button' => array(
-      'variables' => array(
-        'text' => '',
-        'attr' => '',
-        'op' => '',
-        'user_profile' => '',
-      ),
-    ),
-    'fbconnect_user_profile_tab_connected' => array(
-      'variables' => array(
-        'account' => NULL,
-        'conf' => NULL,
-        'uid' => NULL,
-      ),
-      'file' => 'fbconnect.pages.inc',
-    ),
-    'fbconnect_user_profile_tab_unconnected' => array(
-      'variables' => array(
-        'account' => NULL,
-        'conf' => NULL,
-        'uid' => NULL,
-      ),
-      'file' => 'fbconnect.pages.inc',
-    ),
-  );
-}
-
-/**
- * Implements hook_init().
- */
-function fbconnect_init() {
-  fbconnect_render_js();
-  $conf = fbconnect_get_config();
-
-  if ($conf && facebook_client() && user_is_anonymous()) {
-    if ($conf['loginout_mode'] == 'auto') {
-      $fbuid = fbconnect_get_fbuid();
-      if ($fbuid && _is_fbconnect_user($fbuid) && fbconnect_user_profile()) {
-        $form_state = array('values' => array('op' => 'connect'));
-        drupal_form_submit('fbconnect_autoconnect_form', $form_state);
-        drupal_goto($form_state['redirect']);
-      }
-    }
-  }
-  // During registration the cache is disabled
-  if (arg(0) == 'fbconnect' && arg(1) == 'register') {
-    $GLOBALS['conf']['cache'] = FALSE;
-  }
-}
-
-/**
- * Implements hook_user_cancel().
- */
-function fbconnect_user_cancel($edit, $account, $method) {
-  db_delete('fbconnect_users')
-    ->condition('uid', $account->uid)
-    ->execute();
-}
-
-/**
- * Implements hook_user_delete().
- */
-function fbconnect_user_delete($account) {
-  db_delete('fbconnect_users')
-    ->condition('uid', $account->uid)
-    ->execute();
-}
-
-/**
- * Implements hook_user_categories().
- */
-function fbconnect_user_categories() {
-  return array(
-    array(
-      'name' => 'fbconnect',
-      'title' => 'Facebook',
-      'weight' => 3,
-    ),
-  );
-}
-
-/**
- *  Redirects the user to the account linking page.
- *  @todo refactor it
- */
-function fbconnect_redirect_submit($form, &$form_state) {
-  global $user;
-
-  drupal_get_messages('status');
-  $fbuid = fbconnect_get_fbuid();
-  if ($fbuid) {
-    if (fbconnect_register($user->uid, $fbuid)) {
-      drupal_set_message(t('Your Facebook connect link is now active'));
-    }
-    else {
-      if (_get_user_fbuid($user->uid)) {
-        drupal_set_message(t('This user had been already linked another Facebook account.'), 'error');
-      }
-      if (_is_fbconnect_user($fbuid)) {
-        drupal_set_message(t('This Facebook account had been already linked to another user.'), 'error');
-      }
-    }
-  }
-
-  if ($_GET['destination'] == 'fbconnect/link') {
-    unset($_GET['destination']);
-    $form_state['redirect'] = 'user/' . $user->uid . '/edit/fbconnect';
-  }
-}
-
-/**
- * Implements hook_form_alter().
- */
-function fbconnect_form_alter(&$form, $form_state, $form_id) {
-  if (isset($form['account'])) {
-    $form['account']['mail']['#maxlength'] = 320;
-  }
-
-  switch ($form_id) {
-    case 'user_register_form':
-      // Add the FBConnect button to the user registration form, if enabled in
-      // admin settings.
-      if (variable_get('fbconnect_user_reg_display', TRUE)) {
-        $weight = (variable_get('fbconnect_user_reg_location', 'top') == 'top') ? -50 : 50;
-        // create the FBConnect button
-        $attr = array();
-        if (variable_get('fbconnect_fast_reg', 0) && variable_get('fbconnect_reg_options', 0)) {
-          $attr = array('perms' => 'email');
-        }
-        // Don't display if "fast reg" is enabled
-        if (!variable_get('fbconnect_fast_reg', 0)) {
-          $form['fbconnect_button'] = array(
-            '#type' => 'item',
-            '#description' => t('Sign in using Facebook'),
-            '#markup' => fbconnect_render_button($attr),
-            '#weight' => $weight,
-          );
-        }
-      }
-      break;
-
-    case 'user_login':
-      if (!empty($_GET['destination']) && ($_GET['destination'] == 'fbconnect/link')) {
-        drupal_set_message(t('Please log in, in order to link your account with Facebook Connect'));
-        $form['#submit'][] = 'fbconnect_redirect_submit';
-      }
-      // Check if there is a get paramater set(fbconnect=true)
-      if (isset($_GET['fbconnect']) && $_GET['fbconnect'] == 'true') {
-        // Add a function callback to call after user is logged in
-        $form['#submit'][] = 'fbconnect_add_user_afterlogin_submit';
-      }
-      break;
-
-    case 'user_profile_form':
-      if (variable_get('user_pictures', 0) && isset($form['_account'])) {
-        $account = $form['_account']['#value'];
-        if ($account->data['fb_avatar'] && isset($form['picture'])) {
-          $form['picture']['fb_avatar'] = array(
-            '#value' => t('You are currently using your Facebook picture, if you delete or load a new picture, your Facebook picture will no longer be updated.'),
-            '#weight' => 0,
-          );
-          $form['#submit'][] = 'fbconnect_user_edit_submit';
-        }
-      }
-      break;
-
-    case 'user_login_block':
-      if ((empty($_GET['destination']) || $_GET['destination'] != 'fbconnect/link') && facebook_client()) {
-        $form['fbconnect_button'] = array(
-          '#type' => 'item',
-          '#title' => t('Sign in using Facebook'),
-          '#markup' => fbconnect_render_button(),
-          '#weight' => -999,
-          '#id' => 'fbconnect_button',
-        );
-        // Add a function callback to call after user is logged in
-        $form['#submit'][] = 'fbconnect_add_user_afterlogin_submit';
-      }
-      break;
-
-  }
-}
-
-/**
- * Add the user to the fbconnect_users table if they logged in with Facebook
- * @param mixed $form
- * @param mixed $form_state
- */
-function fbconnect_add_user_afterlogin_submit($form, &$form_state) {
-    global $user;
-    $data = fbconnect_get_user_info(array('name', 'email'));
-    //user not logged into Facebook we dont have any information for them, let them log in again
-    if (empty($data)) {
-        module_load_include('inc', 'fbconnect', 'fbconnect.pages');
-        return facebook_login_page($form, $form_state);
-    }
-    $uid = _email_already_exist($data['email']);
-    if ($uid == $user->uid) {
-        //add this user to the fbconnect_users table
-        fbconnect_register($user->uid, $data['id']);
-    }
-    else {
-        drupal_set_message(t('You have logged into the website with an account that does not have the same email as your Facebook account.'));
-    }
-}
-
-/**
- * If the user deletes the photo or load a new picture,
- * we deactivate the automatic import of the Facebook avatar.
- */
-function fbconnect_user_edit_submit($form, &$form_state) {
-  if ($form_state['values']['picture_delete'] || $form_state['values']['picture']) {
-    $user = user_load(arg(1));
-    $data['fb_avatar'] = 0;
-    user_save($user, $data);
-    drupal_set_message(t('Auto import of your Facebook picture has been disabled.'));
-  }
-}
-
-/**
- * Render a custom button to log in via Facebook.
- */
-function fbconnect_render_button($attr = array(), $op = NULL) {
-
-  $user_profile = fbconnect_user_profile();
-  if (!$op) {
-    $op = $user_profile ? 'login' : 'register';
-  }
-
-  list($size) = explode('_', variable_get('fbconnect_button', NULL));
-  $default_attr = array(
-    'text' => variable_get('fbconnect_button_' . $op . '_text', t('Connect')),
-    'size' => $size,
-    'background' => 'dark',
-    'v' => 3,
-  );
-
-  $attr = array_merge($default_attr, $attr);
-  $text = $attr['text'];
-  unset($attr['text']);
-
-  return theme('fbconnect_login_button', array('text' => $text, 'attr' => $attr, 'op' => $op, 'user_profile' => $user_profile));
-}
-
-/**
  * Locates and loads the Facebook PHP SDK library.
  */
 function _facebook_client_load_include() {
@@ -548,7 +222,7 @@ function fbconnect_get_fbuid($check_connected = FALSE) {
       $response = $client->sendRequest($request);
       $fbuid = $response->getGraphUser()->getId();
 
-      if ($check_connected && $fbuid) {
+      if (module_exists('fbconnect_login') && $check_connected && $fbuid) {
         if (_get_user_fbuid($user->uid) != $fbuid) {
           $fbuid = NULL;
         }
@@ -570,17 +244,21 @@ function fbconnect_get_fbuid($check_connected = FALSE) {
 function fbconnect_render_js() {
   global $base_url;
   global $user;
-  $user->fbuid = _get_user_fbuid($user->uid);
+
   $module_path = drupal_get_path('module', 'fbconnect');
 
   if ($config = fbconnect_get_config()) {
     unset($config['secret_api_key']);
-
     $config['fbuid'] = fbconnect_get_fbuid();
     $config['user'] = array(
       'uid' => $user->uid,
-      'fbuid' => @$user->fbuid,
     );
+
+    if (module_exists('fbconnect_login')) {
+      $user->fbuid = _get_user_fbuid($user->uid);
+      $config['fbuid'] = @$user->fbuid;
+    }
+
     drupal_add_js(array('fbconnect' => $config), array('type' => 'setting', 'scope' => JS_DEFAULT));
     drupal_add_js($module_path . '/fbconnect.js');
     drupal_add_css($module_path . '/fbconnect.css');
@@ -650,189 +328,6 @@ function fbconnect_page_alter(&$page) {
 }
 
 /**
- * This form is submitted by javascript when Facebook session is detected.
- */
-function fbconnect_autoconnect_form($form) {
-  $form['#id'] = 'fbconnect-autoconnect-form';
-
-  if (arg(0) == 'user' && in_array(arg(1), array('login', 'register', 'password')) && !isset($_GET['destination'])) {
-    $form['#action'] = url('user');
-  }
-  else {
-    $form['#action'] = url($_GET['q'], array('query' => drupal_get_destination()));
-  }
-
-  if (variable_get('fbconnect_ssl', FALSE)) {
-    $form['#https'] = TRUE;
-  }
-
-  $form['connect'] = array(
-    '#type' => 'submit',
-    '#value' => 'Connect',
-  );
-
-  return $form;
-}
-
-/**
- * Submit handler for autoconnect form.
- */
-function fbconnect_autoconnect_form_submit($form, &$form_state) {
-  $fbuid = fbconnect_get_fbuid();
-  if (!$fbuid) {
-    $error_msg = 'fbconnect_autoconnect_form_submit has been called without valid Facebook session. Check your Application Secret on fbconnect settings page';
-
-    watchdog('fbconnect', $error_msg, array(), WATCHDOG_ALERT, l(t('configure'), 'admin/config/people/fbconnect'));
-    return;
-  }
-
-  if (user_is_anonymous()) {
-    $uid = _is_fbconnect_user($fbuid);
-    $data = fbconnect_get_user_info(array('name', 'email'));
-
-    if ($uid) {
-      $form_state['values']['uid'] = $uid;
-    }
-    $form_state['values']['fbuid'] = $fbuid;
-
-    if (!$uid) {
-      if (variable_get('fbconnect_reg_options', 0) == 1) {
-        if ($uid_from_email = _email_already_exist($data['email'])) {
-          // Connect Facebook to the existing account, and login the user.
-          fbconnect_register($uid_from_email, $data['id']);
-          $form_state['uid'] = $uid_from_email;
-          user_login_submit($form, $form_state);
-        }
-        else {
-          $form_state['redirect'] = array('fbconnect/register/create', drupal_get_destination());
-        }
-      }
-      else {
-        $form_state['redirect'] = array('fbconnect/register/prompt', drupal_get_destination());
-      }
-
-      // #805846 : No linking with FB page appears, when "destination" variable exist in url
-      unset($_GET['destination']);
-    }
-    elseif ($uid) {
-      $account = user_load($uid);
-      //user_external_login($account);
-      $form_state['uid'] = $uid;
-      user_login_submit(array(), $form_state);
-      module_invoke('rules', 'invoke_event', 'fbconnect_user_login', $account);
-    }
-  }
-  else {
-    fbconnect_redirect_submit($form, $form_state);
-  }
-}
-
-/**
- * Check if user already registered in the fbconnect_users table
- *
- * @param Int $fbuid
- *   Facebook user id
- * @return Int $uid
- */
-function _is_fbconnect_user($fbuid) {
-  if ($fbuid) {
-    $query = db_select('fbconnect_users', 'fbu')
-      ->fields('fbu', array('uid'))
-      ->condition('fbuid', $fbuid, '=');
-    $user_registered = $query->execute()->fetchField();
-    if (!empty($user_registered)) {
-      return $user_registered;
-    }
-  }
-}
-
-/**
- * Returns fbuid by uid
- *
- * @param Int $uid
- *   Drupal user id
- * @return Int $fbuid
- */
-function _get_user_fbuid($uid) {
-  if ($uid) {
-    $query = db_select('fbconnect_users', 'fb_u');
-    $query->fields('fb_u', array('fbuid'));
-    $query->condition('uid', $uid, '=');
-    return $query->execute()->fetchField();
-  }
-}
-
-/**
- * Stores user into table fbconnect.
- */
-function fbconnect_register($uid, $fbuid) {
-  if (_get_user_fbuid($uid) || _is_fbconnect_user($fbuid)) {
-    return FALSE;
-  }
-
-  db_merge('fbconnect_users')
-    ->key(array('uid' => $uid))
-    ->fields(array(
-      'fbuid' => $fbuid,
-      'timestamp' => REQUEST_TIME,
-    ))
-    ->execute();
-
-  $account = user_load($uid);
-  $account->fbuid = $fbuid;
-
-  user_save($account, array('fbuid' => $fbuid));
-  module_invoke('rules', 'invoke_event', 'fbconnect_user_registered', $account);
-
-  watchdog('fbconnect', 'User has connected his Facebook account (@fbuid)', array('@fbuid' => $fbuid), WATCHDOG_NOTICE, l(t('edit'), "user/$uid/edit/fbconnect")
-  );
-  return TRUE;
-}
-
-/**
- * Unlink Facebook account from user account.
- */
-function fbconnect_unregister($fbuid) {
-  if (!($client = facebook_client())) {
-    return FALSE;
-  }
-
-  if ($fbuid && $uid = _is_fbconnect_user($fbuid)) {
-    try {
-      try {
-        $request = $client->request('DELETE', '/me/permissions', array(), facebook_get_access_token());
-        $client->getClient()->sendRequest($request);
-      }
-      catch (Exception $e) {
-        drupal_set_message($e->getMessage(), 'error');
-        throw $e;
-      }
-    }
-    catch (Exception $e) {
-      watchdog('fbconnect', 'Exception thrown while using fbconnect_unregister: @code', array('@code' => $e->getMessage()), WATCHDOG_ERROR);
-    }
-
-    db_delete('fbconnect_users')
-      ->condition('fbuid', $fbuid)
-      ->execute();
-    $account = user_load($uid);
-    $account->fbuid = NULL;
-
-    user_save($account, array('fbuid' => NULL));
-    module_invoke('rules', 'invoke_event', 'fbconnect_user_unregistered', $account);
-
-    watchdog('fbconnect', 'User authorization is revoked (@fbuid)', array('@fbuid' => $fbuid), WATCHDOG_NOTICE, l(t('edit'), "user/$uid/edit/fbconnect"));
-
-    return TRUE;
-  }
-
-  $msg = __FUNCTION__ . ' is called with wrong $fbuid(@fbuid) argument ';
-  watchdog('fbconnect', $msg, array('@fbuid' => $fbuid), WATCHDOG_ERROR);
-
-  return FALSE;
-}
-
-/**
  * Make Graph Query.
  *
  * @param string $path
@@ -867,6 +362,7 @@ function _email_already_exist($email) {
   $query = db_select('users', 'u');
   $query->fields('u', array('uid'));
   $query->condition('mail', $email, '=');
+  $query->countQuery();
   return $query->execute()->fetchField();
 }
 
@@ -894,160 +390,6 @@ function fbconnect_get_user_info($fields = array(), $fbuid = NULL) {
   }
 }
 
-/**
- * Implements hook_theme_registry_alter().
- *
- * Override theme functions for things that can be displayed using
- * XFBML. Currently overriding user_picture. We rename
- * the original entries, as we will use them for users without
- * javascript enabled.
- *
- */
-function fbconnect_theme_registry_alter(&$theme_registry) {
-  // Only change image handling if user have the permission to use the image.
-  $conf = fbconnect_get_config();
-  if ($conf['user_pictures'] == 'allow') {
-    // Re-register the original theme function under a new name.
-    $theme_registry['fbconnect_user_picture_orig'] = $theme_registry['user_picture'];
-    // Override theme username
-    $theme_registry['user_picture'] = array(
-      'arguments' => array('account' => NULL),
-      'function' => 'fbconnect_theme_user_picture_override',
-      'preprocess functions' => array(),
-      'theme path' => drupal_get_path('module', 'fbconnect'),
-    );
-  }
-}
-
-/**
- * Our replacement for theme('user_picture', ...)
- */
-function fbconnect_theme_user_picture_override($variables) {
-  $account = $variables['account'];
-  // manually set in nodeapi or comments
-  if (isset($account->fbconnect_picture_preset)) {
-    $preset = $account->fbconnect_picture_preset;
-  }
-  else {
-    // Default user picture preset.
-    $preset = variable_get('fbconnect_pic_size', 'square');
-  }
-
-  $fbuid = _get_user_fbuid($account->uid);
-  
-  // First learn the Facebook id
-  $user_url = url('user/' . $account->uid);
-
-  if ($fbuid && isset($account->data['fb_avatar']) && $account->data['fb_avatar']) {
-    $output = theme('fbconnect_user_picture_override', array('fbuid' => $fbuid, 'account' => $account, 'user_url' => $user_url, 'size' => variable_get('fbconnect_pic_size', $preset), ));
-  }
-  else {
-    $output = theme('fbconnect_user_picture_orig', array('account' => $account)); // Markup without fb_connect.
-  }
-
-  return $output;
-
-}
-
-/**
- * Implements hook_user_view().
- * Change the user profile picture to use the preferred fbconnect preset.
- */
-function fbconnect_user_view($account, $view_mode) {
-  if ($preset = variable_get('fbconnect_pic_size_profile', '')) {
-    $account->fbconnect_picture_preset = $preset;
-    $account->content['user_picture']['#value'] = theme('user_picture', array('account' => $account));
-  }
-}
-
-/**
- * Implements hook_comment_view().
- */
-function fbconnect_comment_view($comment) {
-  if (theme_get_setting('toggle_comment_user_picture')) {
-    // If this is a comment preview, we need to manually add $comment->picture.
-    if (!empty($comment->uid) && !isset($comment->picture)) {
-      $comment->picture = db_query("SELECT picture FROM {users} WHERE uid = :uid", array(':uid' => $comment->uid))->fetchField();
-    }
-    if ($preset = variable_get('fbconnect_pic_size_comments', '')) {
-      $comment->fbconnect_picture_preset = $preset;
-    }
-  }
-}
-
-/**
- * Implements hook_node_view().
- */
-function fbconnect_node_view($node, $view_mode = 'full') {
-  if ($preset = variable_get('fbconnect_pic_size_nodes', '')) {
-    $node->fbconnect_picture_preset = $preset;
-  }
-}
-
-/**
- * Theme function for Facebook connect/login button.
- */
-function theme_fbconnect_login_button($variables) {
-  $op = $variables['op'];
-  $user_profile = $variables['user_profile'];
-  $text = $variables['text'];
-  $attr = $variables['attr'];
-  $attr['data-scope'] = 'email';
-  $destination = request_path();
-
-  if ($op == 'login') {
-    $title = t('Hello %name', array('%name' => $user_profile['name']));
-    $desc = t('Login to this site using your Facebook credentials.');
-    $link = l('<span class="fb_button_text">' . $text . '</span>', 'fbconnect/register/create', array('query' => array('fb-destination' => $destination), 'html' => TRUE, 'attributes' => array('class' => array('fb_button fb_button_' . $attr['size']))));
-    return '<h3>' . $title . '</h3>' . $desc . $link;
-  }
-  else {
-    $url = url('fbconnect/register/create', array('absolute' => TRUE, 'query' => array('fb-destination' => $destination)));
-    $button = '<fb:login-button onlogin="window.location.href = \'' . $url . '\'" ' . drupal_attributes($attr) . '>' . $text . '</fb:login-button>';
-    return $button;
-  }
-}
-
-/**
- * Implements hook_menu_alter().
- */
-function fbconnect_menu_alter(&$callbacks) {
-  $callbacks['user/%user_category/edit/fbconnect']['page callback'] = 'fbconnect_user_identities';
-  $callbacks['user/%user_category/edit/fbconnect']['module'] = 'fbconnect';
-  $callbacks['user/%user_category/edit/fbconnect']['file'] = 'fbconnect.pages.inc';
-}
-
 function _fbconnect_is_excluded_page($path) {
   return drupal_match_path($path, variable_get('fbconnect_exclude_patterns', ''));
 }
-
-/**
- * Implements hook_rdf_namespaces().
- */
-function fbconnect_rdf_namespaces() {
-  return array('fb' => 'http://www.facebook.com/2008/fbml');
-}
-
-/**
- * Implements hook_user_logout().
- */
-function fbconnect_user_logout() {
-  // Redirect user to Facebook logout url.
-  $client = facebook_client();
-
-  if ($client) {
-    try {
-      $request = $client->request('GET', '/me', array(), facebook_get_access_token());
-      $response = $client->getClient()->sendRequest($request);
-      if ($fbuid = $response->getGraphUser()->getId() && variable_get('fbconnect_loginout_mode') == 'auto') {
-        global $base_url;
-        $url = $client->getRedirectLoginHelper()->getLogoutUrl(facebook_get_access_token(), $base_url);
-        session_destroy();
-        drupal_goto($url);
-      }
-    }
-    catch (Exception $e) {
-
-    }
-  }
-}
diff --git a/fbconnect_invite/fbconnect_invite.info b/fbconnect_invite/fbconnect_invite.info
index 324c494..a7296cd 100644
--- a/fbconnect_invite/fbconnect_invite.info
+++ b/fbconnect_invite/fbconnect_invite.info
@@ -1,4 +1,5 @@
-name = Facebook Friends Invite
+name = FBConnect Facebook Friends Invite
 dependencies[] = fbconnect
+dependencies[] = fbconnect_login
 package = "Facebook Social"
 core = 7.x
diff --git a/fbconnect_login/fbconnect_login.info b/fbconnect_login/fbconnect_login.info
new file mode 100644
index 0000000..e0d926d
--- /dev/null
+++ b/fbconnect_login/fbconnect_login.info
@@ -0,0 +1,4 @@
+name = FBConnect Facebook Login
+dependencies[] = fbconnect
+package = "Facebook Social"
+core = 7.x
diff --git a/fbconnect_login/fbconnect_login.install b/fbconnect_login/fbconnect_login.install
new file mode 100644
index 0000000..9162100
--- /dev/null
+++ b/fbconnect_login/fbconnect_login.install
@@ -0,0 +1,73 @@
+<?php
+
+/**
+ * @file
+ * Install, update and uninstall functions for the FBConnect Login module.
+ */
+
+/**
+ * Implements hook_install().
+ */
+function fbconnect_login_install() {
+  _fbconnect_login_change_user_mail_field();
+}
+
+/**
+ * Implements hook_schema().
+ */
+function fbconnect_login_schema() {
+  $schema['fbconnect_users'] = array(
+    'fields' => array(
+      'uid' => array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0,
+      ),
+      'fbuid' => array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0,
+        'size' => 'big',
+      ),
+      'timestamp' => array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0,
+      ),
+    ),
+    'primary key' => array('fbuid'),
+  );
+
+  return $schema;
+}
+
+/**
+ * Implements hook_uninstall
+ */
+function fbconnect_login_uninstall() {
+  _fbconnect_login_change_user_mail_field('uninstall');
+}
+
+/**
+ * Implements hook_schema_alter().
+ * @param array $schema
+ * @todo remove it / create updating tool
+ */
+function fbconnect_login_schema_alter(&$schema) {
+  $schema['users']['fields']['mail']['length'] = 320;
+}
+
+/**
+ * Extend maximum email length to 320 chars
+ */
+function _fbconnect_login_change_user_mail_field($action = 'install') {
+  $schema = array('users' => drupal_get_schema('users'));
+  if ($action == 'install') {
+    fbconnect_login_schema_alter($schema);
+  }
+  db_change_field('users', 'mail', 'mail', $schema['users']['fields']['mail']);
+  //return $res;
+}
diff --git a/fbconnect_login/fbconnect_login.module b/fbconnect_login/fbconnect_login.module
new file mode 100644
index 0000000..fb81128
--- /dev/null
+++ b/fbconnect_login/fbconnect_login.module
@@ -0,0 +1,664 @@
+<?php
+
+/**
+ * @file
+ * Login functionality for Facebook Connect module
+ */
+
+/**
+ * Implements hook_init().
+ */
+function fbconnect_init() {
+  fbconnect_render_js();
+  $conf = fbconnect_get_config();
+
+  if ($conf && facebook_client() && user_is_anonymous()) {
+    if ($conf['loginout_mode'] == 'auto') {
+      $fbuid = fbconnect_get_fbuid();
+      if ($fbuid && _is_fbconnect_user($fbuid) && fbconnect_user_profile()) {
+        $form_state = array('values' => array('op' => 'connect'));
+        drupal_form_submit('fbconnect_login_autoconnect_form', $form_state);
+        drupal_goto($form_state['redirect']);
+      }
+    }
+  }
+  // During registration the cache is disabled
+  if (arg(0) == 'fbconnect' && arg(1) == 'authorize') {
+    $GLOBALS['conf']['cache'] = FALSE;
+  }
+}
+
+/**
+ * Implements hook_menu().
+ */
+function fbconnect_login_menu() {
+  $items['fbconnect/authorize/prompt'] = array(
+    'title' => 'Facebook connect',
+    'page callback' => 'fbconnect_login_prompt_page',
+    'type' => MENU_NORMAL_ITEM,
+    'file' => 'fbconnect_login.pages.inc',
+  );
+  $items['fbconnect/authorize'] = array(
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('fbconnect_login_authorize_page'),
+    'access callback' => TRUE,
+    'type' => MENU_CALLBACK,
+    'file' => 'fbconnect_login.pages.inc',
+  );
+  $items['fbconnect/link'] = array(
+    'page callback' => 'fbconnect_login_link_callback',
+    'access callback' => 'user_is_logged_in',
+    'type' => MENU_CALLBACK,
+    'file' => 'fbconnect_login.pages.inc',
+  );
+
+  return $items;
+}
+
+/**
+ * Implements hook_menu_alter().
+ */
+function fbconnect_login_menu_alter(&$callbacks) {
+  $callbacks['user/%user_category/edit/fbconnect']['page callback'] = 'fbconnect_login_user_identities';
+  $callbacks['user/%user_category/edit/fbconnect']['module'] = 'fbconnect_login';
+  $callbacks['user/%user_category/edit/fbconnect']['file'] = 'fbconnect_login.pages.inc';
+}
+
+/**
+ * Implements hook_block().
+ */
+function fbconnect_login_block_info() {
+  // This example comes from node.module.
+  $blocks['fbconnect_login'] = array(
+    'info' => t('Facebook Login'),
+    'cache' => DRUPAL_NO_CACHE,
+  );
+  return $blocks;
+}
+
+/**
+ * Implements hook_block_view().
+ */
+function fbconnect_login_block_view($delta = '') {
+  global $user;
+  $block = array();
+
+  switch ($delta) {
+    case 'fbconnect_login':
+      if ($user->uid) {
+        return;
+
+      }
+      $block['subject'] = t('Facebook Login');
+      $block['content'] = fbconnect_render_button();
+      break;
+
+  }
+
+  return $block;
+}
+
+/**
+ * Implements hook_rdf_namespaces().
+ */
+function fbconnect_login_rdf_namespaces() {
+  return array('fb' => 'http://www.facebook.com/2008/fbml');
+}
+
+/**
+ * Implements hook_user_logout().
+ */
+function fbconnect_login_user_logout() {
+  // Redirect user to Facebook logout url.
+  $client = facebook_client();
+
+  if ($client) {
+    try {
+      $request = $client->request('GET', '/me', array(), facebook_get_access_token());
+      $response = $client->getClient()->sendRequest($request);
+      if ($fbuid = $response->getGraphUser()->getId() && variable_get('fbconnect_loginout_mode') == 'auto') {
+        global $base_url;
+        $url = $client->getRedirectLoginHelper()->getLogoutUrl(facebook_get_access_token(), $base_url);
+        session_destroy();
+        drupal_goto($url);
+      }
+    }
+    catch (Exception $e) {
+
+    }
+  }
+}
+
+/**
+ * Implements hook_comment_view().
+ */
+function fbconnect_comment_view($comment) {
+  if (theme_get_setting('toggle_comment_user_picture')) {
+    // If this is a comment preview, we need to manually add $comment->picture.
+    if (!empty($comment->uid) && !isset($comment->picture)) {
+      $comment->picture = db_query("SELECT picture FROM {users} WHERE uid = :uid", array(':uid' => $comment->uid))->fetchField();
+    }
+    if ($preset = variable_get('fbconnect_pic_size_comments', '')) {
+      $comment->fbconnect_picture_preset = $preset;
+    }
+  }
+}
+
+/**
+ * Implements hook_node_view().
+ */
+function fbconnect_node_view($node, $view_mode = 'full') {
+  if ($preset = variable_get('fbconnect_pic_size_nodes', '')) {
+    $node->fbconnect_picture_preset = $preset;
+  }
+}
+
+/**
+ * Implements hook_theme().
+ */
+function fbconnect_login_theme() {
+  return array(
+    'fbconnect_login_user_picture_override' => array(
+      'variables' => array(
+        'fbuid' => NULL,
+        'account' => NULL,
+        'user_url' => NULL,
+        'size' => '',
+      ),
+      'file' => 'fbconnect_login.pages.inc',
+    ),
+    'fbconnect_login_button' => array(
+      'variables' => array(
+        'text' => '',
+        'attr' => '',
+        'op' => '',
+        'user_profile' => '',
+      ),
+    ),
+    'fbconnect_login_user_profile_tab_connected' => array(
+      'variables' => array(
+        'account' => NULL,
+        'conf' => NULL,
+        'uid' => NULL,
+      ),
+      'file' => 'fbconnect_login.pages.inc',
+    ),
+    'fbconnect_login_user_profile_tab_unconnected' => array(
+      'variables' => array(
+        'account' => NULL,
+        'conf' => NULL,
+        'uid' => NULL,
+      ),
+      'file' => 'fbconnect_login.pages.inc',
+    ),
+  );
+}
+
+/**
+ * Implements hook_user_cancel().
+ */
+function fbconnect_login_user_cancel($edit, $account, $method) {
+  db_delete('fbconnect_users')
+    ->condition('uid', $account->uid)
+    ->execute();
+}
+
+/**
+ * Implements hook_user_delete().
+ */
+function fbconnect_login_user_delete($account) {
+  db_delete('fbconnect_users')
+    ->condition('uid', $account->uid)
+    ->execute();
+}
+
+/**
+ * Implements hook_user_categories().
+ */
+function fbconnect_login_user_categories() {
+  return array(
+    array(
+      'name' => 'fbconnect',
+      'title' => 'Facebook',
+      'weight' => 3,
+    ),
+  );
+}
+
+/**
+ *  Redirects the user to the account linking page.
+ *  @todo refactor it
+ */
+function fbconnect_login_redirect_submit($form, &$form_state) {
+  global $user;
+
+  drupal_get_messages('status');
+  $fbuid = fbconnect_get_fbuid();
+  if ($fbuid) {
+    if (fbconnect_login_register($user->uid, $fbuid)) {
+      drupal_set_message(t('Your Facebook connect link is now active'));
+    }
+    else {
+      if (_get_user_fbuid($user->uid)) {
+        drupal_set_message(t('This user had been already linked another Facebook account.'), 'error');
+      }
+      if (_is_fbconnect_user($fbuid)) {
+        drupal_set_message(t('This Facebook account had been already linked to another user.'), 'error');
+      }
+    }
+  }
+
+  if ($_GET['destination'] == 'fbconnect/link') {
+    unset($_GET['destination']);
+    $form_state['redirect'] = 'user/' . $user->uid . '/edit/fbconnect';
+  }
+}
+
+/**
+ * Implements hook_form_alter().
+ */
+function fbconnect_login_form_alter(&$form, $form_state, $form_id) {
+  if (isset($form['account'])) {
+    $form['account']['mail']['#maxlength'] = 320;
+  }
+
+  switch ($form_id) {
+    case 'user_register_form':
+      // Add the FBConnect button to the user registration form, if enabled in
+      // admin settings.
+      if (variable_get('fbconnect_user_reg_display', TRUE)) {
+        $weight = (variable_get('fbconnect_user_reg_location', 'top') == 'top') ? -50 : 50;
+        // create the FBConnect button
+        $attr = array();
+        if (variable_get('fbconnect_fast_reg', 0) && variable_get('fbconnect_reg_options', 0)) {
+          $attr = array('perms' => 'email');
+        }
+        // Don't display if "fast reg" is enabled
+        if (!variable_get('fbconnect_fast_reg', 0)) {
+          $form['fbconnect_button'] = array(
+            '#type' => 'item',
+            '#description' => t('Sign in using Facebook'),
+            '#markup' => fbconnect_login_render_button($attr),
+            '#weight' => $weight,
+          );
+        }
+      }
+      break;
+
+    case 'user_login':
+      if (!empty($_GET['destination']) && ($_GET['destination'] == 'fbconnect/link')) {
+        drupal_set_message(t('Please log in, in order to link your account with Facebook Connect'));
+        $form['#submit'][] = 'fbconnect_redirect_submit';
+      }
+      // Check if there is a get paramater set(fbconnect=true)
+      if (isset($_GET['fbconnect']) && $_GET['fbconnect'] == 'true') {
+        // Add a function callback to call after user is logged in
+        $form['#submit'][] = 'fbconnect_add_user_afterlogin_submit';
+      }
+      break;
+
+    case 'user_profile_form':
+      if (variable_get('user_pictures', 0) && isset($form['_account'])) {
+        $account = $form['_account']['#value'];
+        if ($account->data['fb_avatar'] && isset($form['picture'])) {
+          $form['picture']['fb_avatar'] = array(
+            '#value' => t('You are currently using your Facebook picture, if you delete or load a new picture, your Facebook picture will no longer be updated.'),
+            '#weight' => 0,
+          );
+          $form['#submit'][] = 'fbconnect_user_edit_submit';
+        }
+      }
+      break;
+
+    case 'user_login_block':
+      if ((empty($_GET['destination']) || $_GET['destination'] != 'fbconnect/link') && facebook_client()) {
+        $form['fbconnect_button'] = array(
+          '#type' => 'item',
+          '#title' => t('Sign in using Facebook'),
+          '#markup' => fbconnect_login_render_button(),
+          '#weight' => -999,
+          '#id' => 'fbconnect_button',
+        );
+        // Add a function callback to call after user is logged in
+        $form['#submit'][] = 'fbconnect_add_user_afterlogin_submit';
+      }
+      break;
+
+  }
+}
+
+
+/**
+ * Implements hook_theme_registry_alter().
+ *
+ * Override theme functions for things that can be displayed using
+ * XFBML. Currently overriding user_picture. We rename
+ * the original entries, as we will use them for users without
+ * javascript enabled.
+ *
+ */
+function fbconnect_login_theme_registry_alter(&$theme_registry) {
+  // Only change image handling if user have the permission to use the image.
+  $conf = fbconnect_get_config();
+  if ($conf['user_pictures'] == 'allow') {
+    // Re-register the original theme function under a new name.
+    $theme_registry['fbconnect_user_picture_orig'] = $theme_registry['user_picture'];
+    // Override theme username
+    $theme_registry['user_picture'] = array(
+      'arguments' => array('account' => NULL),
+      'function' => 'fbconnect_theme_user_picture_override',
+      'preprocess functions' => array(),
+      'theme path' => drupal_get_path('module', 'fbconnect'),
+    );
+  }
+}
+
+/**
+ * Our replacement for theme('user_picture', ...)
+ */
+function fbconnect_login_theme_user_picture_override($variables) {
+  $account = $variables['account'];
+  // manually set in nodeapi or comments
+  if (isset($account->fbconnect_picture_preset)) {
+    $preset = $account->fbconnect_picture_preset;
+  }
+  else {
+    // Default user picture preset.
+    $preset = variable_get('fbconnect_pic_size', 'square');
+  }
+
+  $fbuid = _get_user_fbuid($account->uid);
+
+  // First learn the Facebook id
+  $user_url = url('user/' . $account->uid);
+
+  if ($fbuid && isset($account->data['fb_avatar']) && $account->data['fb_avatar']) {
+    $output = theme('fbconnect_login_user_picture_override', array('fbuid' => $fbuid, 'account' => $account, 'user_url' => $user_url, 'size' => variable_get('fbconnect_pic_size', $preset), ));
+  }
+  else {
+    $output = theme('fbconnect_login_user_picture_orig', array('account' => $account)); // Markup without fb_connect.
+  }
+
+  return $output;
+
+}
+
+/**
+ * Implements hook_user_view().
+ * Change the user profile picture to use the preferred fbconnect preset.
+ */
+function fbconnect_login_user_view($account, $view_mode) {
+  if ($preset = variable_get('fbconnect_pic_size_profile', '')) {
+    $account->fbconnect_picture_preset = $preset;
+    $account->content['user_picture']['#value'] = theme('user_picture', array('account' => $account));
+  }
+}
+
+/**
+ * Add the user to the fbconnect_users table if they logged in with Facebook
+ * @param mixed $form
+ * @param mixed $form_state
+ */
+function fbconnect_add_user_afterlogin_submit($form, &$form_state) {
+  global $user;
+  $data = fbconnect_get_user_info(array('name', 'email'));
+  //user not logged into Facebook we dont have any information for them, let them log in again
+  if (empty($data)) {
+    module_load_include('inc', 'fbconnect', 'fbconnect.pages');
+    return facebook_login_page($form, $form_state);
+  }
+  $uid = _email_already_exist($data['email']);
+  if ($uid == $user->uid) {
+    //add this user to the fbconnect_users table
+    fbconnect_login_register($user->uid, $data['id']);
+  }
+  else {
+    drupal_set_message(t('You have logged into the website with an account that does not have the same email as your Facebook account.'));
+  }
+}
+
+/**
+ * If the user deletes the photo or load a new picture,
+ * we deactivate the automatic import of the Facebook avatar.
+ */
+function fbconnect_login_user_edit_submit($form, &$form_state) {
+  if ($form_state['values']['picture_delete'] || $form_state['values']['picture']) {
+    $user = user_load(arg(1));
+    $data['fb_avatar'] = 0;
+    user_save($user, $data);
+    drupal_set_message(t('Auto import of your Facebook picture has been disabled.'));
+  }
+}
+
+/**
+ * Stores user into table fbconnect.
+ */
+function fbconnect_login_register($uid, $fbuid) {
+  if (_get_user_fbuid($uid) || _is_fbconnect_user($fbuid)) {
+    return FALSE;
+  }
+
+  db_merge('fbconnect_users')
+    ->key(array('uid' => $uid))
+    ->fields(array(
+      'fbuid' => $fbuid,
+      'timestamp' => REQUEST_TIME,
+    ))
+    ->execute();
+
+  $account = user_load($uid);
+  $account->fbuid = $fbuid;
+
+  user_save($account, array('fbuid' => $fbuid));
+  module_invoke('rules', 'invoke_event', 'fbconnect_user_registered', $account);
+
+  watchdog('fbconnect', 'User has connected his Facebook account (@fbuid)', array('@fbuid' => $fbuid), WATCHDOG_NOTICE, l(t('edit'), "user/$uid/edit/fbconnect")
+  );
+  return TRUE;
+}
+
+/**
+ * Unlink Facebook account from user account.
+ */
+function fbconnect_login_unregister($fbuid) {
+  if (!($client = facebook_client())) {
+    return FALSE;
+  }
+
+  if ($fbuid && $uid = _is_fbconnect_user($fbuid)) {
+    try {
+      try {
+        $request = $client->request('DELETE', '/me/permissions', array(), facebook_get_access_token());
+        $client->getClient()->sendRequest($request);
+      }
+      catch (Exception $e) {
+        drupal_set_message($e->getMessage(), 'error');
+        throw $e;
+      }
+    }
+    catch (Exception $e) {
+      watchdog('fbconnect', 'Exception thrown while using fbconnect_unregister: @code', array('@code' => $e->getMessage()), WATCHDOG_ERROR);
+    }
+
+    db_delete('fbconnect_users')
+      ->condition('fbuid', $fbuid)
+      ->execute();
+    $account = user_load($uid);
+    $account->fbuid = NULL;
+
+    user_save($account, array('fbuid' => NULL));
+    module_invoke('rules', 'invoke_event', 'fbconnect_user_unregistered', $account);
+
+    watchdog('fbconnect', 'User authorization is revoked (@fbuid)', array('@fbuid' => $fbuid), WATCHDOG_NOTICE, l(t('edit'), "user/$uid/edit/fbconnect"));
+
+    return TRUE;
+  }
+
+  $msg = __FUNCTION__ . ' is called with wrong $fbuid(@fbuid) argument ';
+  watchdog('fbconnect', $msg, array('@fbuid' => $fbuid), WATCHDOG_ERROR);
+
+  return FALSE;
+}
+
+/**
+ * This form is submitted by javascript when Facebook session is detected.
+ */
+function fbconnect_login_autoconnect_form($form) {
+  $form['#id'] = 'fbconnect-autoconnect-form';
+
+  if (arg(0) == 'user' && in_array(arg(1), array('login', 'register', 'password')) && !isset($_GET['destination'])) {
+    $form['#action'] = url('user');
+  }
+  else {
+    $form['#action'] = url($_GET['q'], array('query' => drupal_get_destination()));
+  }
+
+  if (variable_get('fbconnect_ssl', FALSE)) {
+    $form['#https'] = TRUE;
+  }
+
+  $form['connect'] = array(
+    '#type' => 'submit',
+    '#value' => 'Connect',
+  );
+
+  return $form;
+}
+
+/**
+ * Submit handler for autoconnect form.
+ */
+function fbconnect_login_autoconnect_form_submit($form, &$form_state) {
+  $fbuid = fbconnect_get_fbuid();
+  if (!$fbuid) {
+    $error_msg = 'fbconnect_login_autoconnect_form_submit has been called without valid Facebook session. Check your Application Secret on fbconnect settings page';
+
+    watchdog('fbconnect', $error_msg, array(), WATCHDOG_ALERT, l(t('configure'), 'admin/config/people/fbconnect'));
+    return;
+  }
+
+  if (user_is_anonymous()) {
+    $uid = _is_fbconnect_user($fbuid);
+    $data = fbconnect_get_user_info(array('name', 'email'));
+
+    if ($uid) {
+      $form_state['values']['uid'] = $uid;
+    }
+    $form_state['values']['fbuid'] = $fbuid;
+
+    if (!$uid) {
+      if (variable_get('fbconnect_reg_options', 0) == 1) {
+        if ($uid_from_email = _email_already_exist($data['email'])) {
+          // Connect Facebook to the existing account, and login the user.
+          fbconnect_login_register($uid_from_email, $data['id']);
+          $form_state['uid'] = $uid_from_email;
+          user_login_submit($form, $form_state);
+        }
+        else {
+          $form_state['redirect'] = array('fbconnect/authorize', drupal_get_destination());
+        }
+      }
+      else {
+        $form_state['redirect'] = array('fbconnect/authorize/prompt', drupal_get_destination());
+      }
+
+      // #805846 : No linking with FB page appears, when "destination" variable exist in url
+      unset($_GET['destination']);
+    }
+    elseif ($uid) {
+      $account = user_load($uid);
+      //user_external_login($account);
+      $form_state['uid'] = $uid;
+      user_login_submit(array(), $form_state);
+      module_invoke('rules', 'invoke_event', 'fbconnect_user_login', $account);
+    }
+  }
+  else {
+    fbconnect_login_redirect_submit($form, $form_state);
+  }
+}
+
+
+/**
+ * Render a custom button to log in via Facebook.
+ */
+function fbconnect_login_render_button($attr = array(), $op = NULL) {
+
+  $user_profile = fbconnect_user_profile();
+  if (!$op) {
+    $op = $user_profile ? 'login' : 'register';
+  }
+
+  list($size) = explode('_', variable_get('fbconnect_button', NULL));
+  $default_attr = array(
+    'text' => variable_get('fbconnect_button_' . $op . '_text', t('Connect')),
+    'size' => $size,
+    'background' => 'dark',
+    'v' => 3,
+  );
+
+  $attr = array_merge($default_attr, $attr);
+  $text = $attr['text'];
+  unset($attr['text']);
+
+  return theme('fbconnect_login_button', array('text' => $text, 'attr' => $attr, 'op' => $op, 'user_profile' => $user_profile));
+}
+
+/**
+ * Check if user already registered in the fbconnect_users table
+ *
+ * @param Int $fbuid
+ *   Facebook user id
+ * @return Int $uid
+ */
+function _is_fbconnect_user($fbuid) {
+  if ($fbuid) {
+    $query = db_select('fbconnect_users', 'fbu')
+      ->fields('fbu', array('uid'))
+      ->condition('fbuid', $fbuid, '=');
+    $user_registered = $query->execute()->fetchField();
+    if (!empty($user_registered)) {
+      return $user_registered;
+    }
+  }
+}
+
+/**
+ * Returns fbuid by uid
+ *
+ * @param Int $uid
+ *   Drupal user id
+ * @return Int $fbuid
+ */
+function _get_user_fbuid($uid) {
+  if ($uid) {
+    $query = db_select('fbconnect_users', 'fb_u');
+    $query->fields('fb_u', array('fbuid'));
+    $query->condition('uid', $uid, '=');
+    return $query->execute()->fetchField();
+  }
+}
+
+/**
+ * Theme function for Facebook connect/login button.
+ */
+function theme_fbconnect_login_button($variables) {
+  $op = $variables['op'];
+  $user_profile = $variables['user_profile'];
+  $text = $variables['text'];
+  $attr = $variables['attr'];
+  $attr['data-scope'] = 'email';
+  $destination = request_path();
+
+  if ($op == 'login') {
+    $title = t('Hello %name', array('%name' => $user_profile['name']));
+    $desc = t('Login to this site using your Facebook credentials.');
+    $link = l('<span class="fb_button_text">' . $text . '</span>', 'fbconnect/authorize', array('query' => array('fb-destination' => $destination), 'html' => TRUE, 'attributes' => array('class' => array('fb_button fb_button_' . $attr['size']))));
+    return '<h3>' . $title . '</h3>' . $desc . $link;
+  }
+  else {
+    $url = url('fbconnect/authorize', array('absolute' => TRUE, 'query' => array('fb-destination' => $destination)));
+    $button = '<fb:login-button onlogin="window.location.href = \'' . $url . '\'" ' . drupal_attributes($attr) . '>' . $text . '</fb:login-button>';
+    return $button;
+  }
+}
diff --git a/fbconnect_login/fbconnect_login.pages.inc b/fbconnect_login/fbconnect_login.pages.inc
new file mode 100644
index 0000000..52cc140
--- /dev/null
+++ b/fbconnect_login/fbconnect_login.pages.inc
@@ -0,0 +1,451 @@
+<?php
+
+/**
+ * @file
+ * User pages callbacks for the fbconnect module.
+ */
+
+/**
+ * Menu callback.
+ * Called when user perform Facebook registration
+ */
+function fbconnect_login_prompt_page() {
+  $conf = fbconnect_get_config();
+  $params = array(
+    '!site_name' => $conf['invite_name'],
+  );
+  $reg_msg = t('Log in to !site_name with Facebook account', $params);
+  $link_msg = t('Click here to connect your existing !site_name account with Facebook', $params);
+
+  $reg_attr = array();
+  if ($conf['fast_reg_mode']) {
+    $reg_attr = array(
+      'attributes' => array(
+        'onclick' => 'Drupal.fbconnect.DoFastRegistration(this); return false;'
+      )
+    );
+  }
+  $items = array(
+    'items' => array(
+      l($reg_msg, 'fbconnect/authorize', $reg_attr),
+      l($link_msg, 'user', array('query' => array('destination' => 'fbconnect/link')))
+    )
+  );
+
+  return theme('item_list', $items);
+}
+
+
+/**
+ * Menu callback.
+ * Logs user into site or redirects.
+ */
+function fbconnect_login_login_page($form, $form_state = array()) {
+  $data = fbconnect_get_user_info(array('name', 'email'));
+  // User not logged into Facebook we dont have any information for them, let them log in.
+  if (empty($data)) {
+    return facebook_login_page($form, $form_state);
+  }
+  // Check if user has already registered with their Facebook account.
+  $user_id = _is_fbconnect_user($data['id']);
+  if ($user_id) {
+    // User already registered Facebook account to site, log them in
+    $form_state['uid'] = $user_id;
+    user_login_submit($form, $form_state);
+    drupal_set_message(t('You have been successfully logged in.'));
+    if (!empty($_GET['destination'])) {
+      drupal_goto($_GET['destination']);
+    } else {
+      drupal_goto('<front>');
+    }
+
+  }
+  elseif (_email_already_exist($data['email'])) {
+    // The user was not found in the fbconnect_users table, but the emial from Facebook might already have an account on coracle.
+    // Redirect user to the login page with a message.
+    drupal_set_message(t('This email address is already registered to an account. Please log in to this account with the username and password provided during registration'));
+    $options = array(
+      'query' => array(
+        'fbconnect' => 'true'
+      )
+    );
+    drupal_goto('user/login', $options);
+  }
+  else {
+    drupal_set_message(t('You do not have an account with this site. Create an account on this site with through Facebook <a href="!here">here</a>'), array('!here' => url('fbconnect/authorize')));
+  }
+}
+
+/**
+ * Menu callback.
+ * Called when user perform Facebook registration
+ */
+function fbconnect_login_authorize_page($form, $form_state = array()) {
+  $conf = fbconnect_get_config();
+  $data = fbconnect_get_user_info(array('name', 'email'));
+  // Check if user has already registered with thier Facebook account
+  $uid = _is_fbconnect_user($data['id']);
+  if ($uid) {
+    // User already registered Facebook account to site, log them in
+    $form_state['uid'] = $uid;
+    user_login_submit($form, $form_state);
+    if (!empty($_GET['fb-destination'])) {
+      drupal_goto($_GET['fb-destination']);
+    } else {
+      drupal_goto('<front>');
+    }
+  }
+  elseif (!empty($data['email']) && _email_already_exist($data['email']) && user_is_anonymous()) {
+    // The user was not found in the fbconnect_users table, but the
+    // email from Facebook might already have an account.
+    // Redirect user to the login page with a message..
+    drupal_set_message(t('This email address is already registered to an account. Please log in to this account with the username and password provided during registration'));
+    $options = array(
+      'query' => array(
+        'fbconnect' => 'true'
+      )
+    );
+    drupal_goto('user/login', $options);
+    return;
+  }
+
+  $form = drupal_retrieve_form('user_register_form', $form_state);
+  $site = $conf['invite_name'];
+
+  // #758918 : prevent users registration if admin has blocked free registration
+  $user_reg_mode = variable_get('user_register', 1);
+  if ($user_reg_mode == 0) {
+    drupal_access_denied();
+    return;
+  }
+
+  $form['account']['name']['#value'] = empty($form_state['input']['name']) ? $data['name'] : $form_state['input']['name'];
+  $form['account']['name']['#parents'] = array('name');
+  if (!empty($data['email'])) {
+    $form['account']['mail']['#value'] = $data['email'];
+    $form['account']['mail']['#parents'] = array('mail');
+  }
+  else {
+    $form['account']['mail']['#value'] = $data['id'] . '@facebook.com';
+    $form['account']['mail']['#parents'] = array('mail');
+  }
+
+  $form['fbconnect'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Facebook Connect'),
+  );
+
+  $form['fbconnect']['fb_visible'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Let my Facebook friends see me on @sitename', array('@sitename' => $site)),
+    '#default_value' => 1,
+  );
+
+  if (variable_get('user_pictures', 0) && $conf['user_pictures'] == 'allow') {
+    $form['fbconnect']['fb_avatar'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Use my Facebook picture as user picture'),
+      '#description' => t('Your picture will be loaded from Facebook.'),
+      '#default_value' => 1,
+    );
+  }
+
+  $form_state['redirect'] = isset($_GET['destination']) ? $_GET['destination'] : '';
+  $form['#validate'][] = 'fbconnect_login_register_form_validate';
+  $form['#submit'][] = 'fbconnect_login_register_form_submit';
+
+  // Fast registration mode, we by pass std drupal reg form.
+  if ($conf['fast_reg_mode']) {
+    $newname = $conf['fast_reg_autoname'] ? _fbconnect_get_username($data['name']) : $data['name'];
+    //drupal_set_message('new name : ' . $newname);
+    $form_state['values']['name'] = $newname;
+    $form_state['values']['mail'] = $data['email'];
+    $form_state['values']['pass'] = user_password();
+    $form_state['values']['status'] = 1;
+    $form_state['values']['fb_visible'] = 1;
+    $form_state['values']['fb_avatar'] = 1;
+
+    //to make validation work have to unset some form fields and settings Taxonomy fields cause errors
+    //TODO:: check this code sure it will need to check for required profile fields and fill them with data
+    foreach ($form as $key => $val) {
+      //unset all cusom fields
+      if (strstr($key, 'field_')) {
+        //unset this custom field
+        unset($form[$key]);
+      }
+    }
+    $form_state['complete form'] = $form;
+    //taxonomy linked fields causes issues with this validate form function
+    drupal_validate_form('user_register', $form, $form_state);
+
+    $success = !form_get_errors();
+    if (!$success) {
+      $dest = array('query' => 'destination=fbconnect/link');
+      drupal_set_message(t('Facebook connect registration failed for the reasons listed. You may register now, or if you already have an account you can <a href="@login">log in</a> now and link your account', array('@login' => url('user/login', $dest))), 'warning');
+      // Append form validation errors below the above warning.
+      $messages = drupal_get_messages('error');
+      foreach ($messages['error'] as $message) {
+        drupal_set_message($message, 'error');
+      }
+
+      drupal_goto('user/register', $dest['query']);
+    }
+
+    fbconnect_login_register_form_submit($form, $form_state);
+  }
+
+  return $form;
+}
+
+function facebook_login_page(&$form, $form_state = array()) {
+  $params['scope'] = 'email';
+  $http_params = drupal_get_query_parameters();
+  $options = array(
+    'absolute' => TRUE,
+    'query' => array('token' => $http_params['token']),
+  );
+  if (module_exists('securepages') && securepages_match('fbconnect/authorize')) {
+    $options['https'] = TRUE;
+  }
+
+  $redirect_uri = url('fbconnect/authorize', $options);
+
+  drupal_goto($redirect_uri);
+}
+
+/**
+ * Returns a name suggestion if the wanted username is already used.
+ */
+function _fbconnect_get_username($name_suggestion) {
+  $query = "SELECT COUNT(name) FROM {users} WHERE name = :user_name";
+  $result = db_query($query, array(':user_name' => $name_suggestion))->fetchField();
+  if ($result) {
+    $counter = variable_get('fbconnect_namecounter', 1);
+    $newname = 'fbuser' . $counter;
+    $counter++;
+    variable_set('fbconnect_namecounter', $counter);
+  }
+  else {
+    $newname = $name_suggestion;
+  }
+  return $newname;
+}
+
+/**
+ * User logged in state validate handler
+ *
+ * @param $form
+ * @param $form_state
+ */
+function fbconnect_login_register_form_validate($form, &$form_state) {
+  $fbuid = fbconnect_get_fbuid();
+  // Make sure we have a Facebook user object by the time we get here otherwise
+  // drop form and exit with error message.
+  if ($fbuid == 0) {
+    drupal_set_message(t('You are not logged into Facebook. Log into Facebook and try again.'), 'error');
+    drupal_goto();
+  }
+}
+
+/**
+ * @param $form
+ * @param $form_state
+ * @throws \Exception
+ */
+function fbconnect_login_register_form_submit($form, &$form_state) {
+  $fbuid = fbconnect_get_fbuid();
+  if (_is_fbconnect_user($fbuid)) {
+    drupal_set_message(t('Retrying registration. Your Facebook account is already assigned to one account on our site.'), 'error');
+    watchdog('fbconnect', 'Error creating account for @fbuid', array('@fbuid' => $fbuid), WATCHDOG_ALERT);
+    drupal_goto();
+  }
+
+  if (variable_get('fbconnect_fast_reg', NULL)) {
+    $user = user_save(NULL, $form_state['values']);
+  }
+  else {
+    $user = $form_state['user'];
+  }
+  if (!$user) {
+    drupal_set_message(t('Error saving user account.'), 'error');
+    drupal_goto();
+  }
+
+  fbconnect_login_register($user->uid, $fbuid);
+  $data['data']['fb_avatar'] = isset($form_state['values']['fb_avatar']) ? $form_state['values']['fb_avatar'] : 0;
+  $data['data']['fb_visible'] = $form_state['values']['fb_visible'];
+  $user = user_save($user, $data);
+
+  $form_state['uid'] = $user->uid;
+  user_login_submit(array(), $form_state);
+  $_SESSION['fbconnect_feed']['type'] = 'register';
+
+  $conf = fbconnect_get_config();
+  if ($conf['fast_reg_mode']) {
+    drupal_goto('user');
+  }
+}
+
+/**
+ * Menu callback fbconnect identities
+ */
+function fbconnect_login_user_identities($form_id, $account) {
+  $conf = fbconnect_get_config();
+  $current_fbuid = fbconnect_get_fbuid(); // fbuid of currently logged in FB user
+  $fbuid = _get_user_fbuid($account->uid); // fbuid of account being edited
+  drupal_set_title(check_plain($account->name));
+  $uid = NULL;
+  if ($fbuid) {
+    $uid = _is_fbconnect_user($fbuid);
+    $account->fbuid = $fbuid;
+  }
+  if (!$uid && !empty($current_fbuid)) {
+    // We have Facebook access for this user but we dont have the user in the fbconnect_users table, add him.
+    fbconnect_register($account->uid, $current_fbuid);
+    $uid = _is_fbconnect_user($current_fbuid);
+  }
+  if (isset($account->fbuid) && $account->fbuid) {
+    return drupal_get_form('fbconnect_login_user_settings_form', $account);
+  }
+  elseif ($uid && $account->uid != $uid) {
+    return theme('fbconnect_login_user_profile_tab_connected', array(
+      'account' => $account,
+      'conf' => $conf,
+      'uid' => $uid
+    ));
+  }
+  else {
+    return theme('fbconnect_login_user_profile_tab_unconnected', array(
+      'account' => $account,
+      'conf' => $conf,
+      'uid' => $uid
+    ));
+  }
+}
+
+/**
+ *  This form allows the user to manage their Facebook connect settings.
+ *
+ *  Some options are visible only if Facebook session is active.
+ */
+function fbconnect_login_user_settings_form($form, $form_state, $account) {
+  $conf = fbconnect_get_config();
+  // These fields manage the visibility of the user,
+  // if the option is enabled, user's Facebook friends
+  // will be able to see his presence on this site
+  $form['fb_visible'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Let my Facebook friends see me on @site', array('@site' => $conf['invite_name'])),
+    '#description' => t('My Facebook friends will be able to see that I own an account on this website.'),
+    '#default_value' => isset($account->data['fb_visible']) ? $account->data['fb_visible'] : NULL,
+  );
+
+  // Whether user picture is enabled,
+  // users can choose to use his Facebook avatar as avatar
+  if ($conf['user_pictures'] == 'allow') {
+    $form['fb_avatar'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Use my Facebook picture as user picture'),
+      '#description' => t('Your picture will be loaded from Facebook.'),
+      '#default_value' => isset($account->data['fb_avatar']) ? $account->data['fb_avatar'] : NULL,
+    );
+  }
+
+  if (!variable_get('user_pictures', 0)) {
+    $form['fb_avatar']['#attributes'] = array(
+      'disabled' => 'disabled',
+    );
+    $form['fb_avatar']['#description'] = t('Enable user picture support in !link', array(
+      '!link' => l(t('User settings'), 'admin/config/people/accounts'),
+    ));
+  }
+
+  $form['account'] = array(
+    '#type' => 'value',
+    '#value' => $account,
+  );
+
+  $form['fbconnect'] = array(
+    '#title' => t('Facebook account'),
+    '#type' => 'fieldset',
+    '#collapsed' => TRUE,
+    '#collapsible' => TRUE,
+  );
+
+  $form['fbconnect']['profile'] = array(
+    '#type' => 'item',
+    '#title' => t('Linked to this Facebook account'),
+    '#markup' => '<fb:name uid="' . $account->fbuid . '" useyou="false" linked="true"></fb:name>',
+  );
+
+  $form['fbconnect']['unlink'] = array(
+    '#type' => 'submit',
+    '#description' => t('Click here to unlink this Facebook account'),
+    '#value' => t('Unlink'),
+    '#access' => !!facebook_client(),
+    // don't show unlink button if fbconnect is'nt properly configured
+  );
+
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Save settings'),
+  );
+  return $form;
+}
+
+/**
+ * Handle post-validation user_settingsForm submission.
+ */
+function fbconnect_login_user_settings_form_submit($form, &$form_state) {
+  $account = $form_state['values']['account'];
+  $data['data']['fb_visible'] = $form_state['values']['fb_visible'];
+  $data['data']['fb_avatar'] = $form_state['values']['fb_avatar'];
+
+  if ($form_state['values']['op'] == $form['fbconnect']['unlink']['#value']) {
+    if (fbconnect_unregister($account->fbuid)) {
+      unset($data);
+    }
+    else {
+      form_set_error('fbconnect', t("Something went wrong. Can't unlink account"));
+    }
+  }
+
+  if (isset($data)) {
+    user_save($account, $data);
+  }
+  drupal_set_message(t('Your configuration options have been saved.'));
+}
+
+/**
+ * Make rendering of Facebook user picture themeable
+ */
+function theme_fbconnect_login_user_picture_override($variables) {
+  $fbuid = $variables['fbuid'];
+  $account = $variables['account'];
+  $user_url = $variables['user_url'];
+  $size = $variables['size'];
+  $protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
+  $path = $protocol . '://graph.facebook.com/' . $fbuid . '/picture?type=' . $size;
+  $image = theme('image', array('path' => $path, 'alt' => $account->name));
+  return '<div class="picture">' . $image . '</div>';
+}
+
+/**
+ * Make rendering of Facebook user profile tab themable
+ */
+function theme_fbconnect_login_user_profile_tab_connected($variables) {
+  $account = $variables['account'];
+  $conf = $variables['conf'];
+  $uid = $variables['uid'];
+  $site_name = $conf['invite_name'] ? $conf['invite_name'] : '';
+  return t("Your current Facebook session is associated to another account on our site.\nYou can logout of Facebook and attempt to associate another Facebook account to your !site_name account, or you need to disconnect the current association.", array('!site_name' => $site_name));
+}
+
+function theme_fbconnect_login_user_profile_tab_unconnected($variables) {
+  return '<div>'
+  . t('Click here to connect your Facebook account')
+  . '</div><div class="fbconnect-button">'
+  . fbconnect_login_render_button()
+  . '</div>';
+}
+
diff --git a/fbconnect_stream_publish/fbconnect_stream_publish.info b/fbconnect_stream_publish/fbconnect_stream_publish.info
index e3ce9d2..d3a6218 100644
--- a/fbconnect_stream_publish/fbconnect_stream_publish.info
+++ b/fbconnect_stream_publish/fbconnect_stream_publish.info
@@ -1,3 +1,3 @@
-name =  Facebook Stream Publish
+name =  FBConnect Stream Publish
 package = "Facebook Social"
 core = 7.x
diff --git a/tests/fbconnect.test b/tests/fbconnect.test
index 9e4714c..05e389a 100644
--- a/tests/fbconnect.test
+++ b/tests/fbconnect.test
@@ -64,7 +64,7 @@ class FbconnectTestCase extends DrupalWebTestCase {
       return;
     }
 
-    parent::setUp('fbconnect', 'fbconnect_test');
+    parent::setUp('fbconnect', 'fbconnect_login', 'fbconnect_test');
 
     foreach ($this->settings as $key => $value) {
       variable_set($key, $value);
@@ -101,10 +101,10 @@ class FbconnectTestCase extends DrupalWebTestCase {
     $this->drupalPost('fbconnect-test/init-fb-session', $edit, 'Submit');
     $this->assertFieldByName('fbuid', $fb_user['id'], 'FB Session inited', 'FBConnect');
 
-    $this->assertTrue(fbconnect_register($account->uid, $fb_user['id']), 'fbconnect_register returned true', 'FBConnect');
+    $this->assertTrue(fbconnect_login_register($account->uid, $fb_user['id']), 'fbconnect_register returned true', 'FBConnect');
     $this->assertNotNull(_get_user_fbuid($account->uid), 'FB Account is connected', 'FBConnect');
 
-    $this->drupalHead('fbconnect/register/create');
+    $this->drupalHead('fbconnect/authorize');
     $this->assertLink('My account', 0, 'User is logged in', 'FBConnect');
   }
 
@@ -125,7 +125,7 @@ class FbconnectTestCase extends DrupalWebTestCase {
     );
     $this->drupalPost('user', $edit, 'Log in', array(
       'query' => array(
-        'destination' => 'fbconnect/register/create',
+        'destination' => 'fbconnect/authorize',
       ),
     ));
     $this->assertLink('My account', 0, 'User is logged in', 'FBConnect');
@@ -147,7 +147,7 @@ class FbconnectTestCase extends DrupalWebTestCase {
       'mail' => $this->randomName() . '@example.com',
     );
 
-    $this->drupalPost('fbconnect/register/create', $edit, 'Create new account');
+    $this->drupalPost('fbconnect/authorize', $edit, 'Create new account');
     $this->assertLink('My account', 0, 'User is logged in', 'FBConnect');
   }
 
@@ -163,7 +163,7 @@ class FbconnectTestCase extends DrupalWebTestCase {
     );
     $this->drupalPost('fbconnect-test/init-fb-session', $edit, 'Submit');
     $this->assertFieldByName('fbuid', $fb_user['id'], 'FB Session inited', 'FBConnect');
-    $this->drupalHead('fbconnect/register/create', array(
+    $this->drupalHead('fbconnect/authorize', array(
       'query' => array(
         'fb-destination' => '',
       ),
