diff --git a/plugins/media/library.js b/plugins/media/library.js
index 6dbbf60..a1f3337 100644
--- a/plugins/media/library.js
+++ b/plugins/media/library.js
@@ -13,9 +13,18 @@
      */
     invoke: function (data, settings, instanceId) {
       if (data.format == 'html') {
-        Drupal.media.popups.mediaBrowser(function (mediaFiles) {
-          Drupal.settings.ckeditor.plugins['media'].mediaBrowserOnSelect(mediaFiles, instanceId);
-        }, settings['global']);
+        if (jQuery(data.node).is('.media-element')) {
+          // Change the view mode for already-inserted media.
+          var mediaFile = Drupal.media.filter.extract_file_info(jQuery(data.node));
+          Drupal.media.popups.mediaStyleSelector(mediaFile, function (mediaFiles) {
+            Drupal.settings.ckeditor.plugins['media'].insertMediaFile(mediaFile, mediaFiles, CKEDITOR.instances[instanceId]);
+          }, settings['global']);
+        }
+        else {
+          Drupal.media.popups.mediaBrowser(function (mediaFiles) {
+            Drupal.settings.ckeditor.plugins['media'].mediaBrowserOnSelect(mediaFiles, instanceId);
+          }, settings['global']);
+        }
       }
     },
 
@@ -40,20 +49,14 @@
         attributes: formattedMedia.options
       });
 
-      this.forceAttributesIntoClass(
-        element,
-        mediaFile.fid,
-        formattedMedia.type,
-        formattedMedia.options
-      );
-
       // Use own wrapper element to be able to properly deal with selections.
       // Check prepareDataForWysiwygMode() in plugin.js for details.
       var wysiwygHTML = Drupal.media.filter.getWysiwygHTML(element);
 
       // Insert element. Use CKEDITOR.dom.element.createFromHtml to ensure our
       // custom wrapper element is preserved.
-      if ( wysiwygHTML.indexOf("<!--MEDIA-WRAPPER-START-") !== -1) {
+      if (wysiwygHTML.indexOf("<!--MEDIA-WRAPPER-START-") !== -1) {
+        ckeditorInstance.plugins.media.mediaLegacyWrappers = true;
         wysiwygHTML = wysiwygHTML.replace(/<!--MEDIA-WRAPPER-START-(\d+)-->(.*?)<!--MEDIA-WRAPPER-END-\d+-->/gi, '');
       } else {
         wysiwygHTML = '<mediawrapper data="">' + wysiwygHTML + '</mediawrapper>';
@@ -63,7 +66,7 @@
       ckeditorInstance.insertElement(editorElement);
 
       // Initialize widget on our html if possible.
-      if (parseFloat(CKEDITOR.version) >= 4.3) {
+      if (parseFloat(CKEDITOR.version) >= 4.3 && typeof(CKEDITOR.plugins.registered.widget) != 'undefined') {
         ckeditorInstance.widgets.initOn( editorElement, 'mediabox' );
       }
     },
diff --git a/plugins/media/plugin.js b/plugins/media/plugin.js
index 78d4696..64e631e 100644
--- a/plugins/media/plugin.js
+++ b/plugins/media/plugin.js
@@ -4,12 +4,19 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 */
 
 /**
-* @file Plugin for inserting images from Drupal media module
-*/
+ * @file Plugin for inserting images from Drupal media module
+ *
+ * @TODO Remove all the legecy media wrapper once it's sure nobody uses that
+ * anymore.
+ */
 ( function() {
   var mediaPluginDefinition = {
     icons: 'media',
     requires: ['button'],
+    // Check if this instance has widget support. All the default distributions
+    // of the editor have the widget plugin disabled by default.
+    hasWidgetSupport: typeof(CKEDITOR.plugins.registered.widget) != 'undefined',
+    mediaLegacyWrappers: false,
 
     // Wrap Drupal plugin in a proxy plugin.
     init: function(editor){
@@ -69,23 +76,32 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
       if (ckeditorversion >= 4.1) {
         // Register allowed tag for advanced filtering.
         editor.filter.allow( 'mediawrapper[!data]', 'MediaWrapper' );
+        // Don't remove the data-file_info attribute added by media!
+        editor.filter.allow( '*[!data-file_info]', 'MediaWrapper' );
         // Objects should be selected as a whole in the editor.
         CKEDITOR.dtd.$object['mediawrapper'] = 1;
       }
       function prepareDataForWysiwygMode(data) {
         data = Drupal.media.filter.replaceTokenWithPlaceholder(data);
+        // Legacy media wrapper.
+        mediaPluginDefinition.mediaLegacyWrappers = (data.indexOf("<!--MEDIA-WRAPPER-START-") !== -1);
         data = data.replace(/<!--MEDIA-WRAPPER-START-(\d+)-->(.*?)<!--MEDIA-WRAPPER-END-\d+-->/gi, '<mediawrapper data="$1">$2</mediawrapper>');
         return data;
       }
       function prepareDataForSourceMode(data) {
-        data = data.replace(/<mediawrapper data="(\d+)">(.*?)<\/mediawrapper>/gi, '<!--MEDIA-WRAPPER-START-$1-->$2<!--MEDIA-WRAPPER-END-$1-->');
+        var replacement = '$2';
+        // Legacy wrapper
+        if (mediaPluginDefinition.mediaLegacyWrappers) {
+          replacement = '<!--MEDIA-WRAPPER-START-$1-->$2<!--MEDIA-WRAPPER-END-$1-->';
+        }
+        data = data.replace(/<mediawrapper data="(.*)">(.*?)<\/mediawrapper>/gi, replacement);
         data = Drupal.media.filter.replacePlaceholderWithToken(data);
         return data;
       }
 
       // Ensure the tokens are replaced by placeholders while editing.
-      if (ckeditorversion >= 4.3) {
-        // CKEditor >=4.3 behaviour.
+      // Check for widget support.
+      if (mediaPluginDefinition.hasWidgetSupport) {
         editor.widgets.add( 'mediabox',
         {
           button: 'Create a mediabox',
@@ -108,7 +124,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
         });
       }
       else if (ckeditorversion >= 4) {
-        // CKEditor >=4.0, <4.3 behaviour.
+        // CKEditor >=4.0
         editor.on('setData', function( event ) {
           event.data.dataValue = prepareDataForWysiwygMode(event.data.dataValue);
         });
@@ -135,7 +151,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
       }
 
       // Provide alternative to the widget functionality introduced in 4.3.
-      if (ckeditorversion < 4.3) {
+      if (!mediaPluginDefinition.hasWidgetSupport) {
         // Ensure tokens instead the html element is saved.
         editor.on('getData', function( event ) {
           event.data.dataValue = prepareDataForSourceMode(event.data.dataValue);
@@ -148,7 +164,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
           var currRange;
           while(currRange = ranges.getNextRange()) {
             var commonAncestor = currRange.getCommonAncestor(false);
-            if (commonAncestor && commonAncestor.getName() == 'mediawrapper') {
+            if (commonAncestor && typeof(commonAncestor.getName) != 'undefined' && commonAncestor.getName() == 'mediawrapper') {
               var range = new CKEDITOR.dom.range( editor.document );
               if (currRange.collapsed === true) {
                 // Don't allow selection within the wrapper element.
@@ -193,7 +209,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
     }
   };
   // Add dependency to widget plugin if possible.
-  if (parseFloat(CKEDITOR.version) >= 4.3) {
+  if (parseFloat(CKEDITOR.version) >= 4.3 && mediaPluginDefinition.hasWidgetSupport) {
     mediaPluginDefinition.requires.push('widget');
   }
   CKEDITOR.plugins.add( 'media', mediaPluginDefinition);
