diff --git a/masonry.module b/masonry.module
index 8349175..b45dc63 100644
--- a/masonry.module
+++ b/masonry.module
@@ -60,6 +60,72 @@ function masonry_default_options() {
 }
 
 /**
+ * Add Masonry javascript and styles.
+ *
+ * @param $settings
+ *   An array of Masonry settings.
+ */
+function masonry_add_masonry($settings = array()) {
+  if (($library = libraries_load('masonry')) && !empty($library['loaded'])) {
+    $settings += masonry_default_options();
+
+    // Add default styling to make grids display properly out-of-the-box
+    $css_margin = ($settings['masonry_width_unit'] == 'px') ? '10px' : '2%';
+    $css_width = ($settings['masonry_width_unit'] == 'px') ? ($settings['masonry_width'] - 20) . 'px' : ($settings['masonry_width'] - 5) . '%';
+    $grid_styles = '
+      ' . (isset($settings['selectors']['grid_styles']) ? $settings['selectors']['grid_styles'] : $settings['selectors']['container']) . ' {
+        float: left;
+        margin: ' . $css_margin . ';
+        width: ' . $css_width . ';
+      }
+    ';
+    drupal_add_css($grid_styles, 'inline');
+    if ($settings['masonry_center']) {
+      $center_styles = '
+        ' . (isset($settings['selectors']['center_styles']) ? $settings['selectors']['center_styles'] : $settings['selectors']['container']) . ' {
+          margin: 0 auto;
+        }
+      ';
+      drupal_add_css($center_styles, 'inline');
+    }
+
+    // Get column width
+    if ($settings['masonry_width_unit'] == 'px') {
+      $column_width = (int) $settings['masonry_width'];
+    }
+    else {
+      $percentage = $settings['masonry_width'] / 100;
+      $column_width = 'function (containerWidth) {
+        return containerWidth * ' . $percentage . ';
+      }';
+    }
+
+    // Initialize Masonry
+    $script = '(function ($) {
+      var $container = $("' . $settings['selectors']['container'] . '");
+      $container.imagesLoaded(function () {
+        $container.masonry({
+          itemSelector: "' . $settings['selectors']['item'] . '",
+          columnWidth: ' . $column_width . ',
+          isAnimated: ' . (int) $settings['masonry_animated'] . ',
+          animationOptions: {
+            duration: ' . (int) $settings['masonry_animated_duration'] . '
+          },
+          isResizable: ' . (int) $settings['masonry_resizable'] . ',
+          isFitWidth: ' . (int) $settings['masonry_center'] . ',
+          gutterWidth: ' . (int) $settings['masonry_gutter'] . ',
+          isRTL: ' . (int) $settings['masonry_rtl'] . '
+        });
+      });
+    })(jQuery);';
+    drupal_add_js($script, array(
+      'type' => 'inline',
+      'scope' => 'footer',
+    ));
+  }
+}
+
+/**
  * Add Masonry options to an existing form.
  *
  * @param $form
diff --git a/masonry_context/context_reaction_masonry.inc b/masonry_context/context_reaction_masonry.inc
index 0a91cf1..82fe8cc 100644
--- a/masonry_context/context_reaction_masonry.inc
+++ b/masonry_context/context_reaction_masonry.inc
@@ -19,16 +19,20 @@ class context_reaction_masonry extends context_reaction {
     // Get default Masonry options
     $settings += masonry_default_options();
 
-    $form['container'] = array(
+    $form['selectors'] = array(
+      '#type' => 'container',
+      '#tree' => TRUE,
+    );
+    $form['selectors']['container'] = array(
       '#type' => 'textfield',
       '#title' => t('Container'),
-      '#default_value' => isset($settings['container']) ? $settings['container'] : NULL,
+      '#default_value' => isset($settings['selectors']['container']) ? $settings['selectors']['container'] : NULL,
       '#description' => t('The jQuery selector of the Masonry container (ie, .region-content-inner).'),
     );
-    $form['item'] = array(
+    $form['selectors']['item'] = array(
       '#type' => 'textfield',
       '#title' => t('Item selector'),
-      '#default_value' => isset($settings['item']) ? $settings['item'] : NULL,
+      '#default_value' => isset($settings['selectors']['item']) ? $settings['selectors']['item'] : NULL,
       '#description' => t('The jQuery selector of the Masonry item container (ie, section).'),
     );
 
@@ -48,70 +52,11 @@ class context_reaction_masonry extends context_reaction {
    * Execution callback.
    */
   function execute() {
-    $script = '';
-    $styles = '';
-
     foreach ($this->get_contexts() as $delta => $context) {
       if (!empty($context->reactions['masonry'])) {
         $settings = $context->reactions['masonry'];
-
-        // Add default styling to make grids display properly out-of-the-box
-        $css_margin = ($settings['masonry_width_unit'] == 'px') ? '10px' : '2%';
-        $css_width = ($settings['masonry_width_unit'] == 'px') ? ($settings['masonry_width'] - 20) . 'px' : ($settings['masonry_width'] - 5) . '%';
-        $styles .= '
-          ' . $settings['container'] . ' ' . $settings['item'] . ' {
-            float: left;
-            margin: ' . $css_margin . ';
-            width: ' . $css_width . ';
-          }
-        ';
-        if ($settings['masonry_center']) {
-          $styles .= '
-            ' . $settings['container'] . ' ' . $settings['item'] . ' {
-              margin: 0 auto;
-            }
-          ';
-        }
-
-        // Get column width
-        if ($settings['masonry_width_unit'] == 'px') {
-          $column_width = (int) $settings['masonry_width'];
-        }
-        else {
-          $percentage = $settings['masonry_width'] / 100;
-          $column_width = 'function (containerWidth) {
-            return containerWidth * ' . $percentage . ';
-          }';
-        }
-
-        $script .= '
-          $("' . $settings['container'] . '").imagesLoaded(function () {
-            $("' . $settings['container'] . '").masonry({
-              itemSelector: "' . $settings['item'] . '",
-              columnWidth: ' . $column_width . ',
-              isAnimated: ' . $settings['masonry_animated'] . ',
-              animationOptions: {
-                duration: ' . (int) $settings['masonry_animated_duration'] . '
-              },
-              isResizable: ' . $settings['masonry_resizable'] . ',
-              isFitWidth: ' . $settings['masonry_center'] . ',
-              gutterWidth: ' . (int) $settings['masonry_gutter'] . ',
-              isRTL: ' . $settings['masonry_rtl'] . '
-            });
-          });
-        ';
+        masonry_add_masonry($settings);
       }
     }
-
-    // Initialize Masonry
-    if (!empty($script) && ($library = libraries_load('masonry')) && !empty($library['loaded'])) {
-      $script = '(function ($) { ' . $script . ' })(jQuery);';
-      drupal_add_css($styles, 'inline');
-      drupal_add_js($script, array(
-        'type' => 'inline',
-        'scope' => 'footer',
-      ));
-    }
   }
 }
-
diff --git a/masonry_formatter/masonry_formatter.module b/masonry_formatter/masonry_formatter.module
index e27c4f1..4afae84 100644
--- a/masonry_formatter/masonry_formatter.module
+++ b/masonry_formatter/masonry_formatter.module
@@ -139,61 +139,12 @@ function masonry_formatter_preprocess_field(&$variables) {
 
   // Display field items in a jQuery Masonry grid
   if (!empty($settings['masonry'])) {
-    if (($library = libraries_load('masonry')) && !empty($library['loaded'])) {
-      // Add default styling to make grids display properly out-of-the-box
-      $css_margin = ($settings['masonry_width_unit'] == 'px') ? '10px' : '2%';
-      $css_width = ($settings['masonry_width_unit'] == 'px') ? ($settings['masonry_width'] - 20) . 'px' : ($settings['masonry_width'] - 5) . '%';
-      $grid_styles = '
-        .field-name-' . $variables['field_name_css'] . ' .field-item {
-          float: left;
-          margin: ' . $css_margin . ';
-          width: ' . $css_width . ';
-        }
-      ';
-      drupal_add_css($grid_styles, 'inline');
-      if ($settings['masonry_center']) {
-        $center_styles = '
-          .field-name-' . $variables['field_name_css'] . ' .field-items {
-            margin: 0 auto;
-          }
-        ';
-        drupal_add_css($center_styles, 'inline');
-      }
-
-      // Get column width
-      if ($settings['masonry_width_unit'] == 'px') {
-        $column_width = (int) $settings['masonry_width'];
-      }
-      else {
-        $percentage = $settings['masonry_width'] / 100;
-        $column_width = 'function (containerWidth) {
-          return containerWidth * ' . $percentage . ';
-        }';
-      }
+    $settings['selectorys'] = array(
+      'container' => ".field-name-{$variables['field_name_css']} .field-item",
+      'item' => '.field-item',
+    );
 
-      // Initialize Masonry
-      $script = '(function ($) {
-        var $container = $(".field-name-' . $variables['field_name_css'] . ' .field-items");
-        $container.imagesLoaded(function () {
-          $container.masonry({
-            itemSelector: ".field-item",
-            columnWidth: ' . $column_width . ',
-            isAnimated: ' . $settings['masonry_animated'] . ',
-            animationOptions: {
-              duration: ' . (int) $settings['masonry_animated_duration'] . '
-            },
-            isResizable: ' . $settings['masonry_resizable'] . ',
-            isFitWidth: ' . $settings['masonry_center'] . ',
-            gutterWidth: ' . (int) $settings['masonry_gutter'] . ',
-            isRTL: ' . $settings['masonry_rtl'] . '
-          });
-        });
-      })(jQuery);';
-      drupal_add_js($script, array(
-        'type' => 'inline',
-        'scope' => 'footer',
-      ));
-    }
+    masonry_add_masonry($settings);
   }
 }
 
diff --git a/masonry_views/masonry_views.module b/masonry_views/masonry_views.module
index 310ae82..b52c7ac 100644
--- a/masonry_views/masonry_views.module
+++ b/masonry_views/masonry_views.module
@@ -18,66 +18,16 @@ function template_preprocess_views_view_masonry(&$vars) {
   // Run preprocess function for unformatted style
   template_preprocess_views_view_unformatted($vars);
 
-  if (($library = libraries_load('masonry')) && !empty($library['loaded'])) {
-    $view = $vars['view'];
-    $view_class = 'view-' . drupal_clean_css_identifier($view->name) . '.view-display-id-' . $view->current_display;
-    $settings = $vars['options'];
+  $view = $vars['view'];
+  $view_class = 'view-' . drupal_clean_css_identifier($view->name) . '.view-display-id-' . $view->current_display;
+  $settings = $vars['options'];
+  $settings['selectors'] = array(
+    'container' => ".{$view_class} .view-content",
+    'grid_styles' => ".{$view_class} .masonry-item",
+    'item' => '.masonry-item',
+  );
 
-    // Add default styling to make grids display properly out-of-the-box
-    $css_margin = ($settings['masonry_width_unit'] == 'px') ? '10px' : '2%';
-    $css_width = ($settings['masonry_width_unit'] == 'px') ? ($settings['masonry_width'] - 20) . 'px' : ($settings['masonry_width'] - 5) . '%';
-    $grid_styles = '
-      .' . $view_class . ' .masonry-item {
-        float: left;
-        margin: ' . $css_margin . ';
-        width: ' . $css_width . ';
-      }
-    ';
-    drupal_add_css($grid_styles, 'inline');
-    if ($settings['masonry_center']) {
-      $center_styles = '
-        .' . $view_class . ' .view-content {
-          margin: 0 auto;
-        }
-      ';
-      drupal_add_css($center_styles, 'inline');
-    }
-
-    // Get column width
-    if ($settings['masonry_width_unit'] == 'px') {
-      $column_width = (int) $settings['masonry_width'];
-    }
-    else {
-      $percentage = $settings['masonry_width'] / 100;
-      $column_width = 'function (containerWidth) {
-        return containerWidth * ' . $percentage . ';
-      }';
-    }
-
-    // Initialize Masonry
-    $script = '(function ($) {
-      var $container = $(".' . $view_class . ' .view-content");
-      $container.imagesLoaded(function () {
-        $container.masonry({
-          itemSelector: ".masonry-item",
-          columnWidth: ' . $column_width . ',
-          isAnimated: ' . $settings['masonry_animated'] . ',
-          animationOptions: {
-            duration: ' . (int) $settings['masonry_animated_duration'] . '
-          },
-          isResizable: ' . $settings['masonry_resizable'] . ',
-          isFitWidth: ' . $settings['masonry_center'] . ',
-          gutterWidth: ' . (int) $settings['masonry_gutter'] . ',
-          isRTL: ' . $settings['masonry_rtl'] . '
-        });
-      }).bind("views_infinite_scroll_updated", function () {
-        $container.masonry("reload");
-      });
-    })(jQuery);';
-    drupal_add_js($script, array(
-      'type' => 'inline',
-      'scope' => 'footer',
-    ));
-  }
+  // Add masonry.
+  masonry_add_masonry($settings);
 }
 
