diff --git a/collapsiblock.module b/collapsiblock.module
index 599c336..b2b232f 100644
--- a/collapsiblock.module
+++ b/collapsiblock.module
@@ -5,6 +5,15 @@
  * Make blocks collapsible.
  */
 
+
+define('COLLAPSIBLOCK_TYPE_NONE', 1);
+define('COLLAPSIBLOCK_TYPE_EXPANDED_BY_DEFAULT', 2);
+define('COLLAPSIBLOCK_TYPE_COLLAPSED_BY_DEFAULT', 3);
+define('COLLAPSIBLOCK_TYPE_ALWAYS_COLLAPSED', 4);
+
+define('COLLAPSIBLOCK_ANIMATION_SLIDE', 1);
+define('COLLAPSIBLOCK_ANIMATION_FADE_SLIDE', 2);
+
 /**
  * Implements hook_init().
  */
@@ -15,11 +24,12 @@ function collapsiblock_init() {
     $collapsiblock_path = drupal_get_path('module', 'collapsiblock');
     $theme_settings = variable_get(str_replace('/', '_', 'theme_' . $current_theme . '_settings'), array());
     $theme_settings = array_merge(collapsiblock_default_settings(), $theme_settings, array());
+    drupal_add_library('system', 'jquery.cookie');
     drupal_add_js(array(
         'collapsiblock' => array(
-            'blocks' => variable_get('collapsiblock_settings', array()),
-            'default_state' => variable_get('collapsiblock_default_state', 1),
-            'slide_type' => variable_get('collapsiblock_slide_type', 1),
+            'blocks' => array_change_key_case(variable_get('collapsiblock_settings', array())),
+            'default_state' => variable_get('collapsiblock_default_state', COLLAPSIBLOCK_TYPE_NONE),
+            'slide_type' => variable_get('collapsiblock_slide_type', COLLAPSIBLOCK_ANIMATION_SLIDE),
             'slide_speed' => variable_get('collapsiblock_slide_speed', 200),
             'block_title' => $theme_settings['collapsiblock_title'],
             'block' => $theme_settings['collapsiblock_block'],
@@ -53,15 +63,23 @@ function collapsiblock_admin_settings($form, &$form_state) {
   $form['collapsiblock_default_state'] = array(
     '#type' => 'radios',
     '#title' => t('Default block collapse behavior'),
-    '#options' => array(1 => t('None.'), 2 => t('Collapsible, expanded by default.'), 3 => t('Collapsible, collapsed by default.'), 4 => t('Collapsible, collapsed all the time.')),
-    '#default_value' => variable_get('collapsiblock_default_state', 1),
+    '#options' => array(
+      COLLAPSIBLOCK_TYPE_NONE => t('None.'),
+      COLLAPSIBLOCK_TYPE_EXPANDED_BY_DEFAULT => t('Collapsible, expanded by default.'),
+      COLLAPSIBLOCK_TYPE_COLLAPSED_BY_DEFAULT => t('Collapsible, collapsed by default.'),
+      COLLAPSIBLOCK_TYPE_ALWAYS_COLLAPSED => t('Collapsible, collapsed all the time.')
+    ),
+    '#default_value' => variable_get('collapsiblock_default_state', COLLAPSIBLOCK_TYPE_NONE),
   );
   $form['collapsiblock_slide_type'] = array(
     '#type' => 'radios',
     '#title' => t('Default animation type'),
-    '#options' => array(1 => t('Slide'), 2 => t('Fade and slide')),
+    '#options' => array(
+      COLLAPSIBLOCK_ANIMATION_SLIDE => t('Slide'),
+      COLLAPSIBLOCK_ANIMATION_FADE_SLIDE => t('Fade and slide')
+    ),
     '#description' => t('Slide is the Drupal default while Fade and slide adds a nice fade effect.'),
-    '#default_value' => variable_get('collapsiblock_slide_type', 1),
+    '#default_value' => variable_get('collapsiblock_slide_type', COLLAPSIBLOCK_ANIMATION_SLIDE),
   );
   $form['collapsiblock_slide_speed'] = array(
     '#type' => 'select',
@@ -135,15 +153,21 @@ function collapsiblock_form_alter(&$form, $form_state, $form_id) {
       '#weight' => -5
     );
 
-    if (isset($settings['block-' . str_replace('_', '-', $form['module']['#value']) . '-' . str_replace('_', '-', $form['delta']['#value'])])) {
-      $default_value = $settings['block-' . str_replace('_', '-', $form['module']['#value']) . '-' . str_replace('_', '-', $form['delta']['#value'])] ? $settings['block-' . str_replace('_', '-', $form['module']['#value']) . '-' . str_replace('_', '-', $form['delta']['#value'])] : variable_get('collapsiblock_default_state', 1);
+    $key = collapsiblock_get_collapsible_key($form['module']['#value'], $form['delta']['#value']);
+    if (isset($settings[$key])) {
+      $default_value = $settings[$key] ? $settings[$key] : variable_get('collapsiblock_default_state', COLLAPSIBLOCK_TYPE_NONE);
     } else {
-      $default_value = 1;
+      $default_value = COLLAPSIBLOCK_TYPE_NONE;
     }
     $form['collapsiblock']['collapse_type'] = array(
       '#type' => 'radios',
       '#title' => t('Block collapse behavior'),
-      '#options' => array(1 => t('None.'), 2 => t('Collapsible, expanded by default.'), 3 => t('Collapsible, collapsed by default.'), 4 => t('Collapsible, collapsed all the time.')),
+      '#options' => array(
+        COLLAPSIBLOCK_TYPE_NONE => t('None.'),
+        COLLAPSIBLOCK_TYPE_EXPANDED_BY_DEFAULT => t('Collapsible, expanded by default.'),
+        COLLAPSIBLOCK_TYPE_COLLAPSED_BY_DEFAULT => t('Collapsible, collapsed by default.'),
+        COLLAPSIBLOCK_TYPE_ALWAYS_COLLAPSED => t('Collapsible, collapsed all the time.')
+      ),
       '#default_value' => $default_value,
     );
   }
@@ -156,7 +180,29 @@ function collapsiblock_form_alter(&$form, $form_state, $form_id) {
  * @see collapsiblock_form_alter()
  */
 function collapsiblock_submit($form, &$form_state) {
+  collapsiblock_set_collapsible_type($form_state['values']['module'], $form_state['values']['delta'], $form_state['values']['collapse_type']);
+}
+
+/**
+ * Set block collapsible type.
+ *
+ * @param $module
+ * @param $block_delta
+ * @param $collapse_type
+ */
+function collapsiblock_set_collapsible_type($module, $block_delta, $collapse_type) {
   $settings = variable_get('collapsiblock_settings', array());
-  $settings['block-' . str_replace('_', '-', $form_state['values']['module']) . '-' . drupal_strtolower(str_replace('_', '-', $form_state['values']['delta']))] = $form_state['values']['collapse_type'];
+  $settings[collapsiblock_get_collapsible_key($module, $block_delta)] = $collapse_type;
   variable_set('collapsiblock_settings', $settings);
 }
+
+/**
+ * Generates the block key ID
+ *
+ * @param $module
+ * @param $block_delta
+ * @return string
+ */
+function collapsiblock_get_collapsible_key($module, $block_delta) {
+  return 'block-' . str_replace('_', '-', $module) . '-' . drupal_strtolower(str_replace('_', '-', $block_delta));
+}
