diff --git a/css/simple_image_rotate.css b/css/simple_image_rotate.css
index 545e2d7..c503bc3 100644
--- a/css/simple_image_rotate.css
+++ b/css/simple_image_rotate.css
@@ -1,38 +1,35 @@
 .image-widget {
     position: relative;
 }
-a.rotate-cw {
+a.rotate-icon {
     position: absolute;
-    top: -13px;
-    left: -13px;
+    top: -15px;
+    left: -15px;
     font-size: 30px;
-    background-color: #FFF;
-    display: block;
-    height: 25px;
-    width: 25px;
-     vertical-align: top; 
-    border-radius: 10px;
-    line-height: 28px;
+    line-height: 30px;
+    color: blue;
+    text-decoration: none;
 }
-.rotate-0 {    
-     -ms-transform: rotate(0deg); /* IE 9 */
-    -webkit-transform: rotate(0deg);
-    transform: rotate(0deg);
+a.rotate-icon:hover {
+    color: black;
+}
+rotate-0 {
+    -webkit-transform: rotate(0deg); /* Chrome, Opera 15+, Safari 3.1+ */
+    -ms-transform: rotate(0deg); /* IE 9 */
+    transform: rotate(0deg); /* Firefox 16+, IE 10+, Opera */
 }
 .rotate-90 {
-     -ms-transform: rotate(90deg); /* IE 9 */
     -webkit-transform: rotate(90deg);
+    -ms-transform: rotate(90deg);
     transform: rotate(90deg);
-    width: 213px;
-    height: 120px;
 }
 .rotate-180 {
-     -ms-transform: rotate(180deg); /* IE 9 */
     -webkit-transform: rotate(180deg);
+    -ms-transform: rotate(180deg);
     transform: rotate(180deg);
 }
 .rotate-270 {
-     -ms-transform: rotate(270deg); /* IE 9 */
     -webkit-transform: rotate(270deg);
+    -ms-transform: rotate(270deg);
     transform: rotate(270deg);
 }
\ No newline at end of file
diff --git a/js/simple_image_rotate.js b/js/simple_image_rotate.js
index 60df6ca..a806bf3 100644
--- a/js/simple_image_rotate.js
+++ b/js/simple_image_rotate.js
@@ -1,28 +1,39 @@
-(function ($) {
-    Drupal.behaviors.simple_image_rotate = {
-        attach: function (context, settings) {
-            $('.rotate-cw').once().click(function () {
-                var weight = $(this).attr('data-weight');
-                var fid = $(this).parent().find('input[name="field_image[und][' + weight + '][fid]"]').val();
-                var rotate_value = parseInt($(this).attr('data-rotate'));
-                //REMOVE EXISTING ROTATE CLASS
-                $(this).parent().siblings('.image-preview').find('img').removeClass('rotate-' + rotate_value);
-                //REMOVE EXISTING HIDDEN ROTATE INPUT
-                $("#field_image_rotate_" + fid).remove();
+/**
+ * @file
+ * A JavaScript file for the theme.
+ *
+ * In order for this JavaScript to be loaded on pages, see the instructions in
+ * the README.txt next to this file.
+ */
 
-                if (rotate_value >= 270) {
-                    rotate_value = 0;
-                } else {
-                    rotate_value = rotate_value + 90;
+// JavaScript should be made compatible with libraries other than jQuery by
+// wrapping it with an "anonymous closure". See:
+// - https://drupal.org/node/1446420
+// - http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth
+(function ($, Drupal, window, document) {
 
-                    $(this).parent().siblings('.image-preview').find('img').addClass('rotate-' + rotate_value);
-                    $(this).parent().append('<input type="hidden" id="field_image_rotate_' + fid + '" name="field_image[und][' + weight + '][rotate]" value="' + rotate_value + '">')
-                }
-                $(this).attr('data-rotate', rotate_value);
+    // To understand behaviors, see https://drupal.org/node/756722#behaviors
+    Drupal.behaviors.rotateImage = {
+        attach: function (context, settings) {
 
+            // Treat click on rotate image icon
+            $(".rotate-icon").off().on("click", function (e) {
 
-                return false;
+                // Prevent default click on rotate icon
+                e.preventDefault();
+                // Current rotate value and thumbnail image
+                var rotate = parseInt($(this).attr("data-rotate")),
+                $image = $(this).closest(".image-widget").find("img");
+                // Remove current rotate CSS class
+                $image.removeClass("rotate-" + rotate);
+                // Update rotate value
+                rotate = rotate < 270 ? rotate + 90 : 0;
+                // Add new rotate CSS class and update rotate value in elements
+                $image.addClass("rotate-" + rotate);
+                $(this).siblings(".rotate").attr("value", rotate);
+                $(this).attr("data-rotate", rotate);
             });
         }
-    }
-})(jQuery);
\ No newline at end of file
+    };
+
+})(jQuery, Drupal, this, this.document);
\ No newline at end of file
diff --git a/simple_image_rotate.info b/simple_image_rotate.info
index 373423d..057366f 100644
--- a/simple_image_rotate.info
+++ b/simple_image_rotate.info
@@ -1,4 +1,5 @@
 name = Simple Image Rotate
-description=Simple image rotation
+description = Simple image rotation
 package = Media
 core = "7.x"
+dependencies[] = image
diff --git a/simple_image_rotate.module b/simple_image_rotate.module
index 8a3d38a..3a459a9 100644
--- a/simple_image_rotate.module
+++ b/simple_image_rotate.module
@@ -1,80 +1,161 @@
 <?php
 
-function simple_image_rotate_form_field_ui_field_edit_form_alter(&$form, &$form_state) {
-  if ($form['#field']['type'] == 'image') {
-    $form['instance']['simple_image_rotate'] = array(
-      '#title' => t('Enable simple rotation'),
-      '#type' => 'checkbox',
-      '#default_value' => isset($form['#instance']['simple_image_rotate']) ? $form['#instance']['simple_image_rotate'] : 0,
-      '#description' => t('Simple image rotate will display rotate icon with image to rotate'),
-      '#weight' => 16,
-    );
-  }
+/**
+ * @file
+ * Allows users to rotate images on node forms.
+ *
+ * To use this feature the "Enable rotate icon" checkbox should be marked for
+ * an image field on its settings page (Administration ┬╗ Structure ┬╗ Content
+ * types ┬╗ Your content type ┬╗ Manage fields ┬╗ Your image field) and an acting
+ * user should have the "rotate images" permission. It will add a small rotate
+ * icon near uploaded image. Clicking to this icon rotates image clockwise with
+ * jQuery only. After saving the node the image file is actually rotated and
+ * renamed. The module requires jQuery library and therefore you will probably
+ * need to install the jQuery Update module.
+ */
+
+/**
+ * Implements hook_permission().
+ */
+function simple_image_rotate_permission() {
+
+  return array(
+    'rotate images' => array(
+      'title' => t('Rotate images'),
+      'description' => t('Allows a user to rotate images.'),
+    ),
+  );
 }
+
 /**
- * Implementation of hook_field_widget_form_alter().
+ * Implements hook_form_FORM_ID_alter().
+ * 
+ * Add enable rotate icon checkbox to image field forms.
+ */
+function simple_image_rotate_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
+
+  $form['instance']['enable_rotate'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Enable rotate icon'),
+    '#description' => t('If checked rotate icon will appear near each uploaded image.'),
+    '#default_value' => isset($form['#instance']['enable_rotate']) ? $form['#instance']['enable_rotate'] : 0,
+    '#weight' => 16,
+  );
+}
+
+/**
+ * Implements hook_field_widget_form_alter().
  */
 function simple_image_rotate_field_widget_form_alter(&$element, &$form_state, $context) {
-  $widget_type = $context['instance']['widget']['type'];
-  if (!empty($context['instance']['simple_image_rotate']) && $context['instance']['simple_image_rotate']) {
-    foreach (element_children($element) as $delta) {
-      if ($element[$delta]['#default_value']['fid'] > 0) {
-        $element[$delta]['#process'][] = 'simple_image_rotate_widget_process';
+
+  // Check if user can rotate images.
+  if ($context['field']['type'] == 'image' && user_access('rotate images')) {
+
+    // Check if image rotation is enabled.
+    if (isset($context['instance']['enable_rotate']) && $context['instance']['enable_rotate']) {
+
+      // Loop through all images.
+      foreach (element_children($element) as $delta) {
+
+        // Skip upload new image button.
+        if (isset($element['#file_upload_delta']) && $delta >= $element['#file_upload_delta'])
+          continue;
+
+        // Add rotate icon and hidden rotate field.
+        $element[$delta]['rotate'] = array(
+          '#type' => 'hidden',
+          '#attributes' => array('class' => 'rotate'),
+          '#value' => 0
+        );
+        $element[$delta]['rotate_icon'] = array(
+          '#markup' => l(t('&#8635;'), '', array(
+            'html' => TRUE,
+            'attributes' => array(
+              'title' => t('Rotate image clockwise'),
+              'class' => array('rotate-icon'),
+              'data-rotate' => 0,
+            ),
+          )),
+        );
       }
+
+      drupal_add_js(drupal_get_path('module', 'simple_image_rotate') . '/js/simple_image_rotate.js');
+      drupal_add_css(drupal_get_path('module', 'simple_image_rotate') . '/css/simple_image_rotate.css');
     }
-    // Add JS and CSS.
-    $path = drupal_get_path('module', 'simple_image_rotate');
-    drupal_add_js($path . '/js/simple_image_rotate.js');
-    drupal_add_css($path . '/css/simple_image_rotate.css');
   }
 }
 
 /**
- * Element #process callback function; process widget type image_image.
+ * Implements hook_entity_presave().
  */
-function simple_image_rotate_widget_process($element, &$form_state, $form) {
-  $rotate_link = l(
-      t('&#8635;'), '', array('html' => TRUE,
-    'attributes' => array(
-      'title' => t('Rotate Clockwise'),
-      'class' => array('rotate-cw'),
-      'data-rotate' => 0,
-      'data-weight' => $element['#parents'][2],
-      'data-fid' => $element['#file']->fid)
-      )
-  );
-  $element['simple_image_rotate'] = array(
-    '#markup' => $rotate_link//"<a class='rotate-cw' data-rotate=0 data-weight='" . $element['#parents'][2] . "' data-fid='" . $element['#file']->fid . "' href='#'>&#8635;</a>",
-  );
-  return $element;
+function simple_image_rotate_entity_presave($entity, $type) {
+  simple_image_rotate_rotate_image($entity, $type);
 }
 
 /**
- * Implementation of hook__entity_update().
+ * Implements hook_profile2_presave().
  */
-function simple_image_rotate_entity_update($entity, $type) {
-  simple_image_rotate_update($entity, $type);
+function simple_image_rotate_profile2_presave($profile) {
+  simple_image_rotate_rotate_image($profile, 'profile2');
 }
 
 /**
- * Implementation of hook__entity_insert().
+ * Rotates image.
  */
-function simple_image_rotate_entity_insert($entity, $type) {
-  simple_image_rotate_update($entity, $type);
-}
+function simple_image_rotate_rotate_image($entity, $type) {
+
+// Load all image fields.
+  $image_fields = array_keys(field_read_fields(array('type' => 'image')));
+  // Loop through all image fields.
+  foreach ($image_fields as $field) {
+
+    // Image field exists in the entity.
+    if (isset($entity->{$field})) {
+
+      // Loop through all images of this field.
+      foreach ($entity->{$field}[LANGUAGE_NONE] as $delta => $value) {
+
+        // If rotate angle is indicated.
+        if (isset($value['rotate']) && $value['rotate']) {
+
+          // Load image file.
+          $file = file_load($value['fid']);
+
+          // Get new filename for rotated image.
+          $new_uri = $file->uri;
+          $file_counter = 1;
+          if (preg_match('#_r([\d]+)\.([^.]+)$#i', $new_uri, $matches)) {
+            $file_counter = $matches[1] + 1;
+            $new_uri = str_replace($matches[0], '_r' . $file_counter . '.jpg', $new_uri);
+          }
 
-function simple_image_rotate_update($entity, $type) {
-  if ($type == 'node') {
-    $image_fields = db_query("SELECT fci.field_name FROM {field_config} fc, {field_config_instance} fci
-                            WHERE fc.field_name = fci.field_name AND fc.`type` = 'image' AND fci.bundle = :bundle", array(':bundle' => $entity->type));
-
-    foreach ($image_fields as $field) {
-      $field_instance = field_info_instance('node', $field->field_name, $entity->type);
-      if (isset($field_instance['simple_image_rotate']) && $field_instance['simple_image_rotate']) {
-        if (isset($entity->{$field->field_name}[LANGUAGE_NONE]) && count($entity->{$field->field_name}[LANGUAGE_NONE]) > 0) {
-          foreach ($entity->{$field->field_name}[LANGUAGE_NONE] as $key => $image) {
-            if (isset($image['rotate'])) {
-              sir_rotate_image($image['fid'], $image['rotate'], 'field_image');
+          // Increment filename counter if filename is occupied.
+          while (file_exists($new_uri)) {
+            $pos = strrpos($new_uri, '.');
+            $new_uri = substr_replace($new_uri, '_r' . $file_counter, $pos, 0);
+          }
+
+          // Move image file to a new location.
+          if (file_move($file, $new_uri, 'FILE_EXIST_ERROR')) {
+
+            // Update file object after moving file.
+            $file = file_load($value['fid']);
+
+            // Rotate image and save image object.
+            $image = image_load($file->uri);
+            image_rotate($image, $value['rotate']);
+            if (image_save($image)) {
+
+              // Reload image to get new width, height and filesize.
+              $image = image_load($file->uri);
+
+              // Update image width and height in entity.
+              $entity->{$field}[LANGUAGE_NONE][$delta]['width'] = $image->info['width'];
+              $entity->{$field}[LANGUAGE_NONE][$delta]['height'] = $image->info['height'];
+
+              // Update file.
+              $file->filesize = $image->info['file_size'];
+              file_save($file);
             }
           }
         }
@@ -82,24 +163,3 @@ function simple_image_rotate_update($entity, $type) {
     }
   }
 }
-
-function sir_rotate_image($fid, $rotate = 90, $field_name) {
-  $file = file_load($fid);
-
-  $img = image_load($file->uri);
-  image_rotate($img, $rotate);
-  $result = image_save($img);
-  if ($result) {
-    $uri = $file->uri;
-    $ext = substr($uri, -3); // Change this if you expect some weird extensions like .jpeg !
-    $new_uri = substr($uri, 0, -4) . '_1' . '.' . $ext;
-    file_move($file, $new_uri);
-    // it is not completely ok to pass php variables into sql just like that, but we do it here since the input is always safe in my case
-    $nid = db_query("SELECT entity_id FROM {field_data_{$field_name}} WHERE {$field_name}_fid=:fid", array(':fid' => $fid))->fetchField();
-    db_query("UPDATE {file_managed} SET filesize=:size WHERE fid=:fid", array(':size' => $img->info['file_size'], ':fid' => $fid));
-    db_query("UPDATE {field_data_{$field_name}} SET {$field_name}_width=:width, {$field_name}_height=:height WHERE {$field_name}_fid=:fid LIMIT 1", array(':width' => $img->info['width'], ':height' => $img->info['height'], ':fid' => $fid));
-    cache_clear_all("field:node:$nid", 'cache_field');
-
-    image_path_flush($img->source);
-  }
-}
