diff --git a/contact_form_blocks.css b/contact_form_blocks.css
index f1cbd02..a8439ec 100644
--- a/contact_form_blocks.css
+++ b/contact_form_blocks.css
@@ -1,5 +1,11 @@
-/* $Id: contact_form_blocks.css,v 1.1.4.1 2009/08/19 22:23:54 fuerst Exp $ */
-/* Reset the fixed width of input form text fields to use a dynamic width depending on the parent elements */
-div.sidebar div.block-contact_form_blocks input[type=text] {
+/**
+ * @file
+ * Stylesheet for the Contact Form Blocks module.
+ */
+
+/* Reset the fixed width of input form text fields to use a dynamic width
+ * depending on the parent elements.
+ */
+.block-contact-form-blocks input[type=text] {
   width: 94%;
-}
\ No newline at end of file
+}
diff --git a/contact_form_blocks.info b/contact_form_blocks.info
index 3e4a7a2..6680150 100644
--- a/contact_form_blocks.info
+++ b/contact_form_blocks.info
@@ -1,8 +1,4 @@
-; $Id:
 name = Contact Form Blocks
 description = Makes your site-wide contact forms available as Drupal Blocks.
 core = 7.x
-version = "7.x-1.0-dev"
 dependencies[] = contact
-
-
diff --git a/contact_form_blocks.install b/contact_form_blocks.install
new file mode 100644
index 0000000..5eaf0e8
--- /dev/null
+++ b/contact_form_blocks.install
@@ -0,0 +1,22 @@
+<?php
+
+/**
+ * @file
+ * Install, update and uninstall functions for the contact_form_blocks module.
+ */
+
+/**
+ * Implements hook_uninstall().
+ */
+function contact_form_blocks_uninstall() {
+  variable_del('contact_form_blocks_site_wide_categories');
+}
+
+/**
+ * Clean-up variable. Remove disabled values.
+ */
+function contact_form_blocks_update_7000() {
+  $categories = variable_get('contact_form_blocks_site_wide_categories', array());
+  $categories = array_filter($categories);
+  variable_set('contact_form_blocks_site_wide_categories', $categories);
+}
diff --git a/contact_form_blocks.module b/contact_form_blocks.module
index 817a6be..6e68ae7 100644
--- a/contact_form_blocks.module
+++ b/contact_form_blocks.module
@@ -1,38 +1,14 @@
 <?php
-
 /**
- * Implementation of hook_block_info().
+ * @file
+ * Makes your site-wide contact forms available as Drupal Blocks.
  *
- * @return void
- * @author Bernhard Fürst
- */
-function contact_form_blocks_block_info() {
-  return contact_form_blocks_get_blocks();
-}
-
-/**
- * Implementation of hook_block_view().
- *
- * @return void
  * @author Bernhard Fürst
+ * @author Artem cross Shymko
  */
 
-function contact_form_blocks_block_view($delta = '') {  
-  if ($delta == 0) {
-    return contact_form_global_form_content();
-  } 
-  else {
-    return contact_form_blocks_content($delta);
-  }
-}
-
 /**
- * Implementation of hook_menu().
- *
- * Provide the Settings page
- *
- * @return void
- * @author Bernhard Fürst
+ * Implements hook_menu().
  */
 function contact_form_blocks_menu() {
   $items = array();
@@ -42,192 +18,220 @@ function contact_form_blocks_menu() {
     'description' => 'Contact blocks settings',
     'page arguments' => array('contact_form_blocks_settings'),
     'page callback' => 'drupal_get_form',
-    'access arguments' => array('administer site-wide contact form'),
+    'access arguments' => array('administer contact forms'),
   );
 
   return $items;
 }
 
 /**
- * Implementation of hook_admin_paths().
+ * Implements hook_admin_paths().
  *
- * Provide the Admin paths
- *
- * @return void
- * @author Bernhard Fürst
+ * Provides the Admin paths for overlay module.
  */
 function contact_form_blocks_admin_paths() {
   $paths = array(
-    'admin/config/contact_form_blocks' => TRUE, 
+    'admin/config/system/contact_form_blocks' => TRUE,
   );
   return $paths;
 }
 
 /**
- * Get the HTML code for the contact form with specific category
- *
- * @param int $category The contact.cid of the contact form category from the database
- * @return array Content/title array for Blocks
- * @author Bernhard Fürst
+ * Implements hook_block_info().
  */
-function contact_form_blocks_content($category) {
-  // Get data about existing contact categories (formatted in block array)
+function contact_form_blocks_block_info() {
+  $enabled_categories = variable_get('contact_form_blocks_site_wide_categories', array());
   $categories = contact_form_blocks_get_categories();
+  // Allow blocks for enabled categories only.
+  $categories = array_intersect_key($categories, $enabled_categories);
+  // Add block for site-wide contact form.
+  $categories = array(0 => t('Global')) + $categories;
 
-  // Extract block title
-  $content['subject'] = $categories[$category];
+  $blocks = array();
+  foreach ($categories as $cid => $info) {
+    $blocks[$cid]['info'] = sprintf('%s: %s', t('Contact form'), $info);
+  }
+  return $blocks;
+}
 
-  // Don't allow misuse by using the contact form too often
-  if (!flood_is_allowed('contact', variable_get('contact_hourly_threshold', 3))) {
-    $content['content'] = t("You cannot send more than %number messages per hour. Please try again later.", array('%number' => variable_get('contact_hourly_threshold', 3)));
-    return $content;
+/**
+ * Implements hook_block_view().
+ */
+function contact_form_blocks_block_view($delta = '') {
+  if ($delta == '0') {
+    return contact_form_global_form_content();
   }
+  else {
+    $enabled_categories = variable_get('contact_form_blocks_site_wide_categories', array());
+    if (isset($enabled_categories[$delta])) {
+      // Display block only for enabled categories.
+      return contact_form_blocks_content($delta);
+    }
+  }
+}
 
-  // Need to know about the category in contact_form_blocks_form_contact_mail_page_alter()
-  $_SESSION['contact_form_blocks_form_category'] = $category;
+/**
+ * Builds HTML code for the contact form with specific category.
+ *
+ * @param int $category
+ *   The contact.cid of the contact form category.
+ *
+ * @return
+ *   Content/title array for Blocks
+ */
+function contact_form_blocks_content($category) {
+  // Check if flood control has been activated for sending e-mails.
+  $limit = variable_get('contact_threshold_limit', 5);
+  $window = variable_get('contact_threshold_window', 3600);
+  if (!flood_is_allowed('contact', $limit, $window) && !user_access('administer contact forms')) {
+    return array('content' => t("You cannot send more than %limit messages in @interval. Try again later.", array('%limit' => $limit, '@interval' => format_interval($window))));
+  }
 
-  // Load the contact module's code to get the contact form
+  // Get all existing contact categories.
+  $categories = contact_form_blocks_get_categories();
+  // Set the block title.
+  $content['subject'] = $categories[$category];
+
+  // Load the contact module's code to get the contact form.
   module_load_include('inc', 'contact', 'contact.pages');
 
   // Append the category to the original "contact_site_form" form_id to be able
-  // to handle multiple contact forms on one page
-  $mail = array();
-  $mail_form = drupal_build_form('contact_site_form', $mail);
-  $mail_form['cid']['#value'] = $category;
-  $mail_form['cid']['#access'] = FALSE;
-  $content['content'] = drupal_render($mail_form);
-
-  // Add some modifications to the CSS
+  // to handle multiple contact forms on one page.
+  $form_state = array();
+  $form = drupal_build_form('contact_site_form', $form_state);
+  $form['cid'] = array('#type' => 'value', '#value' => $category);
+  $content['content'] = drupal_render($form);
+
+  // Add some modifications to the CSS.
   drupal_add_css(drupal_get_path('module', 'contact_form_blocks') . '/contact_form_blocks.css');
 
   return $content;
 }
 
 /**
- * Get the HTML code for the contact form 
- * 
- * @return array Content/title array for Blocks
- * @author Artem cross Shymko
+ * Builds HTML code for the global contact form.
+ *
+ * @return
+ *   Content/title array for Block.
  */
 function contact_form_global_form_content() {
-  // Don't allow misuse by using the contact form too often
-  if (!flood_is_allowed('contact', variable_get('contact_hourly_threshold', 3))) {
-    $content['content'] = t("You cannot send more than %number messages per hour. Please try again later.", array('%number' => variable_get('contact_hourly_threshold', 3)));
-    return $content;
+  // Check if flood control has been activated for sending e-mails.
+  $limit = variable_get('contact_threshold_limit', 5);
+  $window = variable_get('contact_threshold_window', 3600);
+  if (!flood_is_allowed('contact', $limit, $window) && !user_access('administer contact forms')) {
+    return array('content' => t("You cannot send more than %limit messages in @interval. Try again later.", array('%limit' => $limit, '@interval' => format_interval($window))));
   }
 
-  // Load the contact module's code to get the contact form
+  // Load the contact module's code to get the contact form.
   module_load_include('inc', 'contact', 'contact.pages');
-  
-  $mail = array();
-  $mail_form = drupal_build_form('contact_site_form', $mail);
-  $content['content'] = drupal_render($mail_form);
 
-  // Add some modifications to the CSS
+  $form_state = array();
+  $form = drupal_build_form('contact_site_form', $form_state);
+  $content['content'] = drupal_render($form);
+
+  // Add some modifications to the CSS.
   drupal_add_css(drupal_get_path('module', 'contact_form_blocks') . '/contact_form_blocks.css');
 
   return $content;
 }
 
 /**
- * Get the contact form categories
+ * Gets all the contact categories from the database.
  *
- * @return array Contact form categories formatted as block array
- * @author Bernhard Fürst
+ * @return
+ *  Keyed array of categories.
  */
-function contact_form_blocks_get_categories($cids = NULL) {
-  $categories = array();
-  $result = db_query('SELECT cid, category FROM {contact} ORDER BY weight, category');
-  foreach ($result as $row) {
-    if (!empty($cids) AND !in_array($row->cid, $cids)) {
-      continue;
-    }
-    $categories[$row->cid] = $row->category;
+function contact_form_blocks_get_categories() {
+  $categories = &drupal_static(__FUNCTION__);
+  if (!isset($categories)) {
+    // Get all the contact categories from the database.
+    $categories = db_select('contact', 'c')
+      ->addTag('translatable')
+      ->fields('c', array('cid', 'category'))
+      ->orderBy('weight')
+      ->orderBy('category')
+      ->execute()
+      ->fetchAllKeyed();
   }
   return $categories;
 }
 
 /**
- * Format the contact form categories as block array
- *
- * @return void
- * @author Bernhard Fürst
- */
-function contact_form_blocks_get_blocks() {
-  $categories = array(t('Global'));
-  $categories = array_merge($categories, contact_form_blocks_get_categories());
-
-  $blocks = array();
-  foreach ($categories as $cid => $info) {
-    $blocks[$cid]['info'] = sprintf('%s: %s', t('Contact form'), $info);
-  }
-  return $blocks;
-}
-
-/**
- * Build the settings form
- *
- * @return void
- * @author Bernhard Fürst
+ * Form builder; the admin settings form.
  */
-function contact_form_blocks_settings() {
+function contact_form_blocks_settings($form) {
   $categories = contact_form_blocks_get_categories();
 
-  // Warning if no contact category being set
+  // Warning if no contact categories being set.
   if (empty($categories)) {
-    drupal_set_message(filter_xss(t("You need to !link first before being able to set them here.")), array('!link' => l(t("create contact form categories"), "admin/build/contact/add")));
+    // Message same as in contact_site_form().
+    drupal_set_message(t('The contact form has not been configured. <a href="@add">Add one or more categories</a> to the form.', array('@add' => url('admin/structure/contact/add'))));
     return array();
   }
 
-  $form = array();
-
   $form['contact_form_blocks_categories'] = array(
     '#type' => 'checkboxes',
     '#title' => t('Categories to be shown in the site wide contact form'),
-    '#default_value' => variable_get('contact_form_blocks_site_wide_categories', array_keys($categories)),
+    '#default_value' => variable_get('contact_form_blocks_site_wide_categories', array()),
     '#options' => $categories,
   );
-
-  $form['submit'] = array(
-  '#type' => 'submit',
-  '#value' => t('Save configuration'),
-  );
+  $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
 
   return $form;
 }
 
-
 /**
- * Save the settings
+ * Form submission handler for contact_form_blocks_settings().
  *
- * @return void
- * @author Bernhard Fürst
+ * @see contact_form_blocks_settings()
  */
-
 function contact_form_blocks_settings_submit($form, &$form_state) {
-  variable_set('contact_form_blocks_site_wide_categories', $form_state['values']['contact_form_blocks_categories']);
-  drupal_set_message(t('The settings have been saved'));
+  // Filter only enabled values.
+  $enabled_categories = array_filter($form_state['values']['contact_form_blocks_categories']);
+  variable_set('contact_form_blocks_site_wide_categories', $enabled_categories);
+  drupal_set_message(t('The configuration options have been saved.'));
 }
 
 /**
- * Implements hook_form_alter() 
- * 
- * @author Artem cross Shymko
+ * Implements hook_form_FORM_ID_alter().
+ *
+ * @see contact_site_form()
  */
-function contact_form_blocks_form_alter(&$form, &$form_state, $form_id) {
-  if ($form_id == 'contact_site_form') {
-    $form['#submit'][] = 'contact_form_blocks_submit';
-  }
+function contact_form_blocks_form_contact_site_form_alter(&$form, &$form_state, $form_id) {
+  $form['#submit'][] = '_contact_form_blocks_submit';
 }
 
 /**
  * Extra form submission handler for contact_site_form().
- * 
- * @author Artem cross Shymko
  */
-function contact_form_blocks_submit($form, &$form_state) {
-  // Redirect user back to page with form
-  // instead of baseurl
+function _contact_form_blocks_submit($form, &$form_state) {
+  // Redirect user back to page with form instead of frontpage.
   $destination = drupal_get_destination();
-  $form_state['redirect'] = $destination['destination'];		
-}
\ No newline at end of file
+  $form_state['redirect'] = $destination['destination'];
+  // Clear all caches to renew block list.
+  cache_clear_all();
+}
+
+/**
+ * Implements hook_form_FORM_ID_alter().
+ *
+ * @see contact_category_delete_form()
+ */
+function contact_form_blocks_contact_category_delete_form_alter(&$form, &$form_state, $form_id) {
+  $form['#submit'][] = '_contact_form_blocks_category_delete';
+}
+
+/**
+ * Extra form submission handler for contact_category_delete_form().
+ */
+function _contact_form_blocks_category_delete($form, &$form_state) {
+  $enabled_categories = variable_get('contact_form_blocks_site_wide_categories', array());
+  $contact = $form['contact']['#value'];
+  if (isset($enabled_categories[$contact['cid']])) {
+    // Remove deleted category from allowed blocks.
+    unset($enabled_categories[$contact['cid']]);
+    variable_set('contact_form_blocks_site_wide_categories', $enabled_categories);
+    // Re-build cache to remove block for deleted category.
+    cache_clear_all();
+  }
+}
