diff --git a/webform_mailchimp.inc b/webform_mailchimp.inc
index 9508a9e..61064f8 100644
--- a/webform_mailchimp.inc
+++ b/webform_mailchimp.inc
@@ -18,10 +18,13 @@ function _webform_defaults_mailchimp() {
     'mandatory' => 0,
     'extra' => array(
       'title_display' => 0,
+      'field_prefix' => '',
+      'field_suffix' => '',
       'description' => '',
       'attributes' => array(),
       'mailchimp_list' => '',
       'use_existing_email_field' => '',
+      'private' => FALSE,
     ),
   );
 }
@@ -32,10 +35,10 @@ function _webform_defaults_mailchimp() {
 function _webform_theme_mailchimp() {
   return array(
     'webform_display_mailchimp' => array(
-      'arguments' => array('element' => NULL),
+      'render element' => 'element',
     ),
     'webform_mailchimp_email' => array(
-      'arguments' => array('element' => NULL),
+      'render element' => 'element',
     ),
   );
 }
@@ -44,39 +47,34 @@ function _webform_theme_mailchimp() {
  * Implementation of _webform_edit_component().
  */
 function _webform_edit_mailchimp($component) {
-  module_load_include('php', 'mailchimp', 'MCAPI.class');
-  
   drupal_add_js(drupal_get_path('module', 'webform_mailchimp') . '/webform_mailchimp.js');
 
   $node = node_load($component['nid']);
   $form = array();
 
   // Only show the list selection forms if a Mailchimp API key is set.
-  $api_key = variable_get('mailchimp_api_key', FALSE);
   $options = array('none' => 'Select list');
-  if ($api_key) {
-    $q = new MCAPI($api_key);
-
-    if (!$q->errorCode) {
-      $lists = $q->lists();
-      if (!empty($lists)) {
-        foreach ($lists as $list) {
-          $options[$list['id']] = $list['name'];
-        }
-      }
-      else {
-        drupal_set_message(t('You do not have any Mailchimp lists defined.', 'error'));
+  $q = mailchimp_get_api_object();
+  if ($q) {
+    $lists = mailchimp_get_lists();
+    if (!empty($lists)) {
+      foreach ($lists as $key => $list) {
+        $options[$list['id']] = $list['name'];
       }
     }
+    else {
+      drupal_set_message(t('You do not have any Mailchimp lists defined.', 'error'));
+    }
   }
   else {
-    drupal_set_message(t('You need to configure your Mailchimp API key before using this component.'), 'error');
+    drupal_set_message(t('Could not get valid Mailchimp API object'), 'error');
   }
 
   $form['extra']['mailchimp_list'] = array(
     '#type' => 'select',
     '#title' => t('Choose list'),
     '#default_value' => !empty($component['extra']['mailchimp_list']) ? $component['extra']['mailchimp_list'] : 0,
+      $component['extra']['mailchimp_list'] : 0,
     '#description' => t('Choose which list that the user can subscribe to.'),
     '#options' => $options,
     '#element_validate' => array('_webform_mailchimp_list_validate'),
@@ -91,7 +89,6 @@ function _webform_edit_mailchimp($component) {
     '#element_validate' => array('_webform_mailchimp_user_email_validate'),
   );
 
-
   $options = array('mailchimp_field' => 'Create field');
 
   // Fetches existing components, checks if any of them are e-mail fields.
@@ -128,9 +125,33 @@ function _webform_edit_mailchimp($component) {
     '#description' => t('If using an existing field, make the checkbox checked by default.'),
     '#default_value' => !empty($component['extra']['checkbox_checked_by_default']) ? $component['extra']['checkbox_checked_by_default'] : 0,
     '#weight' => 3,
+  );
+
+  $form['extra']['checkbox_hidden'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Hide the checkbox.'),
+    '#description' => t('Check this if users do not need to be able to set or unset wether they want to subscribe. Note that you still need to check wether the checkbox should be checked by default.'),
+    '#default_value' => !empty($component['extra']['checkbox_hidden']) ? $component['extra']['checkbox_hidden'] : 0,
+    '#weight' => 4,
     '#suffix' => '</div>',
   );
 
+  $form['extra']['mergefields'] = array(
+    '#type' => 'textarea',
+    '#title' => t('Mailchimp merge fields'),
+    '#description' => t('Enter one key|value pair per line. Use the MailChimp field tag as the key and the webform field key as the value. For example "FNAME|firstname"'),
+    '#default_value' => !empty($component['extra']['mergefields']) ? $component['extra']['mergefields'] : '',
+    '#weight' => 5,
+  );
+
+  $form['extra']['interestfields'] = array(
+    '#type' => 'textarea',
+    '#title' => t('Mailchimp interests fields'),
+    '#description' => t('Enter one key|value pair per line. Use the MailChimp group name as the key and the webform field (checkboxes or dropdown) key as the value. For example "FOOD|types_of_food_you_like"'),
+    '#default_value' => !empty($component['extra']['interestfields']) ? $component['extra']['interestfields'] : '',
+    '#weight' => 6,
+  );
+
   return $form;
 }
 
@@ -161,16 +182,23 @@ function _webform_render_mailchimp($component, $value = NULL, $filter = TRUE) {
   }
 
   $element = array(
-    '#title'             => htmlspecialchars($component['name'], ENT_QUOTES),
+    //'#type'              => 'mailchimp',
+    '#title'             => $filter ? _webform_filter_xss($component['name']) : $component['name'],
     '#title_display'     => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
+    //'#default_value'     => $filter ? _webform_filter_values($component['value'], NULL, NULL, NULL, FALSE) : $component['value'],    
     '#required'          => $component['mandatory'],
     '#weight'            => $component['weight'],
-    '#prefix'            => '<div class="webform-component-'. $component['type'] .'" id="webform-component-'. $component['form_key'] .'">',
-    '#suffix'            => '</div>',
+    //'#prefix'            => '<div class="webform-component-'. $component['type'] .'" id="webform-component-'. $component['form_key'] .'">',
+    //'#suffix'            => '</div>',
+    //'#field_prefix'      => empty($component['extra']['field_prefix']) ? NULL : ($filter ? _webform_filter_xss($component['extra']['field_prefix']) : $component['extra']['field_prefix']),
+    //'#field_suffix'      => empty($component['extra']['field_suffix']) ? NULL : ($filter ? _webform_filter_xss($component['extra']['field_suffix']) : $component['extra']['field_suffix']),
+    '#description' => $filter ? _webform_filter_descriptions($component['extra']['description']) : $component['extra']['description'],    
+    '#attributes' => $component['extra']['attributes'],
+    //'#theme_wrappers' => array('webform_element'),
     '#webform_component' => $component,
-    '#theme'             => 'webform_mailchimp_email',
-    '#pre_render'        => array('webform_element_title_display'),
-    '#element_validate'  => array('_webform_validate_mailchimp_email'),
+    //'#theme'             => 'webform_mailchimp_email',
+    //'#pre_render'        => array('webform_element_title_display'),
+    //'#element_validate'  => array('_webform_validate_mailchimp_email'),
   );
 
   // Creates a field if the user hasn't chosen another email field.
@@ -180,19 +208,28 @@ function _webform_render_mailchimp($component, $value = NULL, $filter = TRUE) {
       '#description'       => 'Enter your e-mailaddress.',
       '#default_value'     => _webform_filter_values($component['value']),
       '#attributes'        => $component['extra']['attributes'],
-      '#size'              => $size,
+      //'#size'              => $size,
     );
   }
   else {
-    $element['mailchimp_signup'] = array(
-      '#type'              => 'checkbox',
-      '#title'             => !empty($component['extra']['checkbox_label']) ? htmlspecialchars($component['extra']['checkbox_label'], ENT_QUOTES) : t('Subscribe to newsletter'),
-      '#default_value'     => !empty($component['extra']['checkbox_checked_by_default']) ? $component['extra']['checkbox_checked_by_default'] : 0,
-      '#attributes'        => $component['extra']['attributes'],
-    );
+    // Creates a checkbox to subscribe, or a hidden form element if configured
+    if ($component['extra']['checkbox_hidden'] == 1) {
+      $element['mailchimp_signup'] = array(
+        '#type'              => 'hidden',
+        '#default_value'     => !empty($component['extra']['checkbox_checked_by_default']) ? $component['extra']['checkbox_checked_by_default'] : 0,
+      );
+    }
+    else {
+      $element['mailchimp_signup'] = array(
+        '#type'              => 'checkbox',
+        '#title'             => !empty($component['extra']['checkbox_label']) ? _webform_filter_xss($component['extra']['checkbox_label']) : t('Subscribe to newsletter'),
+        '#default_value'     => !empty($component['extra']['checkbox_checked_by_default']) ? $component['extra']['checkbox_checked_by_default'] : 0,
+        '#attributes'        => $component['extra']['attributes'],
+      );
+    }
   }
-
   $mailchimp_list = $component['extra']['mailchimp_list'];
+
   $element['mailchimp_list'] = array(
       '#prefix' => '',
       '#type' => 'value',
@@ -229,7 +266,7 @@ function _webform_display_mailchimp($component, $value, $format = 'html') {
   return array(
     '#title' => $component['name'],
     '#weight' => $component['weight'],
-    '#theme' => 'webform_display_textfield',
+    '#theme' => 'webform_display_mailchimp',
     '#theme_wrappers' => $format == 'html' ? array('webform_element', 'webform_element_wrapper') : array('webform_element_text'),
     '#post_render' => array('webform_element_wrapper'),
     '#field_prefix' => $component['extra']['field_prefix'],
@@ -243,7 +280,8 @@ function _webform_display_mailchimp($component, $value, $format = 'html') {
 /**
  * Theme the webform element.
  */
-function theme_webform_mailchimp_email($element) {
+function theme_webform_mailchimp_email($variables) {
+  $element = $variables['element'];
   if (form_get_error($element)) {
     $element['mailchimp_email_address']['#attributes']['class'] = 'error';
   }
@@ -255,15 +293,23 @@ function theme_webform_mailchimp_email($element) {
 /**
  * Format the output of data for this component.
  */
-function theme_webform_display_mailchimp($element) {
+function theme_webform_display_mailchimp($variables) {
+  $element = $variables['element'];
+  $prefix = $element['#format'] == 'html' ? '' : $element['#field_prefix'];
+  $suffix = $element['#format'] == 'html' ? '' : $element['#field_suffix'];
   $value = $element['#format'] == 'html' ? check_plain($element['#value']) : $element['#value'];
-  return $value !== '' ? ($value) : ' ';
+  return $value !== '' ? ($prefix . $value . $suffix) : ' ';
 }
 
 /**
  * Implementation of _webform_submit_component().
  */
 function _webform_submit_mailchimp($component, $value) {
+  $return_val = NULL;
+  
+  if (!empty($value['mailchimp_signup'])){
+    $return_val = array(0 => $value['mailchimp_signup']);
+  }
   if (!empty($value['mailchimp_email_address'])) {
     $return_val = array(0 => $value['mailchimp_email_address']);
   }
@@ -275,18 +321,21 @@ function _webform_submit_mailchimp($component, $value) {
  * Implementation of _webform_analysis_component().
  */
 function _webform_analysis_mailchimp($component, $sids = array()) {
-  $placeholders = count($sids) ? array_fill(0, count($sids), "'%s'") : array();
-  $sidfilter = count($sids) ? " AND sid in (" . implode(",", $placeholders) . ")" : "";
-  $query = 'SELECT data ' .
-    ' FROM {webform_submitted_data} ' .
-    ' WHERE nid = %d ' .
-    ' AND cid = %d ' . $sidfilter;
+  $query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC))
+    ->fields('wsd', array('data'))
+    ->condition('nid', $component['nid'])
+    ->condition('cid', $component['cid']);
+
+  if (count($sids)) {
+    $query->condition('sid', $sids, 'IN');
+  }
+
   $nonblanks = 0;
   $submissions = 0;
   $wordcount = 0;
 
-  $result = db_query($query, array_merge(array($component['nid'], $component['cid']), $sids));
-  while ($data = db_fetch_array($result)) {
+  $result = $query->execute();
+  foreach ($result as $data) {
     if (drupal_strlen(trim($data['data'])) > 0) {
       $nonblanks++;
       $wordcount += str_word_count(trim($data['data']));
diff --git a/webform_mailchimp.info b/webform_mailchimp.info
index 8aceb62..9f569ce 100644
--- a/webform_mailchimp.info
+++ b/webform_mailchimp.info
@@ -1,7 +1,6 @@
-
 name = Webform Mailchimp
 description = Creates a Mailchimp Webform component.
-core = 6.x
+core = 7.x
 dependencies[] = webform
 dependencies[] = mailchimp
-package = Webform
\ No newline at end of file
+package = Webform
diff --git a/webform_mailchimp.module b/webform_mailchimp.module
index e29cf7c..53260ec 100644
--- a/webform_mailchimp.module
+++ b/webform_mailchimp.module
@@ -1,4 +1,5 @@
 <?php
+
 /**
  * Implementation of hook_webform_component_info().
  */
@@ -37,7 +38,7 @@ function webform_mailchimp_webform_submission_insert($node, $submission) {
         // Need to know if we should look for our own email field or another.
         if ($field['extra']['use_existing_email_field'] != 'mailchimp_field') {
           // If using existing field, check if the checkbox was checked
-          if ($submission->data[$key]['value']['mailchimp_signup'] == 1) {
+          if ($submission->data[$key]['value'][0] == 1) {
             // Loop through components again to find our email field
             foreach($node->webform['components'] AS $key2 => $field2) {
               if ($field2['form_key'] == $field['extra']['use_existing_email_field']) {
@@ -51,15 +52,84 @@ function webform_mailchimp_webform_submission_insert($node, $submission) {
         else {
           $email_address = $submission->data[$key]['value'][0];
         }
+
+        // Retrieve mergefields and create a merge array with key webform key and value Mailchimp merge tag
+        if (!empty($field['extra']['mergefields'])) {
+          $mergefields_key_array = array();
+          $keyvaluepairs = explode("\n", $field['extra']['mergefields']);
+
+          foreach ($keyvaluepairs AS $keyvalue) {
+            $keyvalue = trim($keyvalue);
+            $keyvalue = explode('|', $keyvalue);
+            if (is_array($keyvalue) && !empty($keyvalue[0]) && !empty($keyvalue[1])) {
+              $mergefields_key_array[$keyvalue[1]] = $keyvalue[0];
+            }
+          }
+        }
+
+        // Retrieve interestfields and create a merge array with key webform key and value Mailchimp group name
+        if (!empty($field['extra']['interestfields'])) {
+          $groupfields_key_array = array();
+          $keyvaluepairs = explode("\n", $field['extra']['interestfields']);
+
+          foreach ($keyvaluepairs AS $keyvalue) {
+            $keyvalue = trim($keyvalue);
+            $keyvalue = explode('|', $keyvalue);
+            if (is_array($keyvalue) && !empty($keyvalue[0]) && !empty($keyvalue[1])) {
+              $groupfields_key_array[$keyvalue[1]] = $keyvalue[0];
+            }
+          }
+        }
+
+      }
+    }
+
+    $mergefields_replacements = array();
+    // Create the mergefield array
+    if (!empty($mergefields_key_array) && is_array($mergefields_key_array)) {
+      foreach ($node->webform['components'] AS $key => $field) {
+        if (!empty($mergefields_key_array[$field['form_key']])) {
+          // This is probably a bit to easy... The delta value is not taken into account
+          $mergefields_replacements[$mergefields_key_array[$field['form_key']]] = $submission->data[$key]['value'][0];
+        }
+      }
+    }
+
+    $groupfields_replacements = array();
+    // Create the mergefield array
+    if (!empty($groupfields_key_array) && is_array($groupfields_key_array)) {
+      foreach ($node->webform['components'] AS $key => $field) {
+        if (!empty($groupfields_key_array[$field['form_key']])) {
+          // We are dealing with checkboxes, dropdowns and have received the id instead of the raw value
+          // So we need to extract the value and send that instead
+          $choices = explode("\n", $field['extra']['items']);
+          $sorted_choices = array();
+          foreach ($choices as $choice_key => $choice_val) {
+            $id_name = explode('|', trim($choice_val));
+            $sorted_choices[$id_name[0]] = $id_name[1];
+          }
+
+          foreach ($submission->data[$key]['value'] AS $filled_out_value) {
+            $groupfields_replacements[$groupfields_key_array[$field['form_key']]][] = str_replace(",", "\,", $sorted_choices[$filled_out_value]);
+          }
+        }
+      }
+      foreach ($groupfields_replacements AS $groupname => $values_array) {
+        $mergefields_replacements['GROUPINGS'][] = array(
+          'name' => $groupname,
+          'groups' => implode(',', $values_array),
+        );
       }
     }
 
     // If we have an email address and a list is set, try to subscribe the user.
     if ($email_address != '' && $mailchimp_list != '') {
       // Fetches available lists, so we get the list object.
-      $lists = _mailchimp_get_available_lists($user);
-      if (isset($lists[$mailchimp_list])) {
-        _mailchimp_subscribe_user($lists[$mailchimp_list], $email_address, array(), TRUE, NULL);
+      $lists = mailchimp_lists_get_available_lists($user);
+      foreach ($lists as $key => $list) {
+        if ($list->mc_list_id == $mailchimp_list) {
+          mailchimp_subscribe_user($lists[$key], $email_address, $mergefields_replacements, TRUE, NULL);
+        }
       }
     }
   }
