diff --git a/README.txt b/README.txt
index b6d5e7f..c1821e9 100644
--- a/README.txt
+++ b/README.txt
@@ -1,7 +1,8 @@
-This module lets you show a preview of PDF files uploaded through FileField.
+This module lets you show a preview of PDF files uploaded through File field.
 This module uses ImageMagick to extract the first page of a PDF file to a JPEG
 image which is used as PDF preview link to the file. If imagecache module is
 available, you can select any imagecache preset for the PDF preview image link.
+
 Instead of convert pdf files onto a set of images like PDF to ImageField does,
 and lost any connection to the PDF file, PDFPreview extract only the first page
 and uses it as link to PDF file. The images are stored on a configurable folder
diff --git a/pdfpreview.info b/pdfpreview.info
index 9887ebc..2a2fa12 100644
--- a/pdfpreview.info
+++ b/pdfpreview.info
@@ -1,7 +1,5 @@
 name = PDF Preview
 description = Show a snapshot of the first page of pdf files attached to nodes.
-package = CCK
-core = 6.x
-dependencies[] = filefield
-dependencies[] = imageapi_imagemagick
-dependencies[] = imagecache
+package = Media
+core = 7.x
+dependencies[] = imagemagick
diff --git a/pdfpreview.install b/pdfpreview.install
index 77272b5..acefe04 100644
--- a/pdfpreview.install
+++ b/pdfpreview.install
@@ -10,6 +10,7 @@
  */
 function pdfpreview_uninstall() {
   variable_del('pdfpreview_pathtoimages');
+  variable_del('pdfpreview_previewsize');
   variable_del('pdfpreview_tag');
   variable_del('pdfpreview_description');
 }
diff --git a/pdfpreview.module b/pdfpreview.module
index 3e851c5..e6dbe69 100644
--- a/pdfpreview.module
+++ b/pdfpreview.module
@@ -1,25 +1,24 @@
 <?php
-
 /**
  * @file
- * This module creates a formatter for CCK filefields that
- * shows a snapshot of the first page of pdf files as link
- * to the file.
+ * This file contains hooks for the pdfpreview module
  *
- * @defgroup pdfpreview PDFPreview: generate snapshots of PDF files to use as link to them.
  * @author Juanjo Garcia <juanjo.gcia@gmail.com>
- *  Main file for the pdfpreview module
+ * @author Florian Auer <floeschie@gmail.com>
  *
  */
 
+
 /**
- * Implements hook_field_formatter_info().
+ * Implements hook_field_formatter_info()
+ *
+ * @todo What's the imagecache thing doing?
  */
 function pdfpreview_field_formatter_info() {
   $formatters = array(
     'default' => array(
       'label' => t('PDF Preview'),
-      'field types' => array('filefield'),
+      'field types' => array('file'),
       'description' => t('Displays an snapshot of the first page of the PDF'),
     ),
   );
@@ -33,97 +32,102 @@ function pdfpreview_field_formatter_info() {
       );
     }
   }
+
   return $formatters;
 }
 
+
 /**
- * Implements hook_theme().
+ * Implements hook_field_formatter_view()
  *
- * We declare our theme functions according to the array keys in hook_field_formatter_info
+ * @see _pdfpreview_create_preview()
  */
-function pdfpreview_theme() {
-  $theme = array(
-    'pdfpreview_formatter_default' => array(
-      'function' => 'theme_pdfpreview_formatter',
-      'arguments' => array('element' => NULL),
-    ),
-  );
+function pdfpreview_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
+  $element = array();
 
-  if (module_exists('imagecache')) {
-    foreach (imagecache_presets() as $preset) {
-      $theme['pdfpreview_formatter_'. $preset['presetname'] . '][pdfpreview'] = array(
-        'function' => 'theme_pdfpreview_formatter',
-        'arguments' => array('element' => NULL),
+  foreach ($items as $delta => $item) {
+    $file = (object) $item;
+    $element[$delta]['#file'] = $file;
+    if ($item['filemime'] == 'application/pdf') {
+      $item['preview'] = _pdfpreview_create_preview($file);
+      $element[$delta] = array(
+        '#file' => $file,
+        '#preview' => $item['preview'],
+        '#markup' => theme($display['type'], array('item' => $item, 'field' => $instance)),
       );
+    } else {
+      module_load_include('inc', 'file', 'file.field');
+      $display['type'] = 'file_default';
+      $tmp = file_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, array($item), $display);
+      $element[$delta] = $tmp[0];
     }
   }
 
-  return $theme;
+  return $element;
 }
 
-/**
- * Theming functions for our formatters
- */
-function theme_pdfpreview_formatter($element) {
-  $output = '';
-  $item = $element['#item'];
-  if ($item['filemime'] != 'application/pdf' or $item['list'] != 1) return '';
-  $output_dir = file_directory_path() . '/' . variable_get('pdfpreview_pathtoimages', 'pdfpreview');
-  $output_filename = $output_dir . '/' . md5($item['fid']) .'.jpg';
-  if (!file_exists($output_dir)) {
-    if (!mkdir($output_dir)) {
-      drupal_set_message(t('Error creating directory @dir', array('%dir' => $output_directory)), 'error');
-      watchdog('pdfpreview', 'Error creating directory @dir', array('%dir' => $output_directory), WATCHDOG_ERROR);
-      return '';
-    }
-    $message = t('The directory %dir has been created', array('%dir' => $output_directory));
-    watchdog('pdfpreview', 'The directory %dir has been created', array('%dir' => $output_directory));
-    drupal_set_message($message, 'status');
-  }
-  elseif (!is_dir($output_dir)) {
-    $message = t('The path %dir is not a directory', array('%dir' => $output_directory));
-    watchdog('pdfpreview', 'The path %dir is not a directory', array('%dir' => $output_directory), WATCHDOG_ERROR);
-    drupal_set_message($message, 'error');
-    return '';
-  }
-  if (!file_exists($output_filename) && function_exists('_imageapi_imagemagick_convert_exec')) {
-    _imageapi_imagemagick_convert_exec($item['filepath'] ."[0] $output_filename", $cmd_output, $cmd_errors);
-  }
-  $output = '<img src="/' . $output_filename . '" title="' . $item['data']['description'] . '" alt="' . $item['data']['description'] .'" />';
-  if (list($namespace, $presetname) = explode('][', $element['#formatter'], 2)) {
-    if ($preset = imagecache_preset_by_name($namespace)) {
-      $output = theme('imagecache', $namespace, $output_filename, $item['data']['alt'], $item['data']['title']);
-    }
-  }
-  $tag = variable_get('pdfpreview_tag', 'span');
-  $description = (variable_get("pdfpreview_description", 1))?"<$tag class=\"pdfpreview-description\">{$item['data']['description']}</$tag>" : "";
-  return "<div class=\"pdfpreview\" id=\"pdfpreview-{$item['fid']}\">
-    <$tag class=\"image-wrapper\"><a href=\"/{$item['filepath']}\" title=\"{$item['data']['description']}\">
-    $output</a></$tag>$description</div>";
-}
 
 /**
  * Implements hook_menu()
  */
 function pdfpreview_menu() {
   $items = array(
-      'admin/settings/pdfpreview' => array(
-          'title' => 'Configure PDF Preview',
-          'description' => 'Configure PDF Preview settings',
-          'access arguments' => array('administer content'),
-          'page callback' => 'drupal_get_form',
-          'page arguments' => array('pdfpreview_admin_settings'),
-          'type' => MENU_NORMAL_ITEM,
-      ),
-      );
+    'admin/config/media/pdfpreview' => array(
+      'title' => 'PDF Preview',
+      'description' => 'Configure PDF Preview settings',
+      'access arguments' => array('administer content'),
+      'page callback' => 'drupal_get_form',
+      'page arguments' => array('_pdfpreview_admin_settings'),
+      'type' => MENU_NORMAL_ITEM,
+    ),
+  );
+
   return $items;
 }
 
+
+/**
+ * Implements hook_theme().
+ *
+ */
+function pdfpreview_theme() {
+  $theme = array(
+    'file_default' => array(
+      'function' => 'theme_pdfpreview_formatter',
+      'variables' => array('element' => NULL),
+    ),
+    'default' => array(
+      'function' => 'theme_pdfpreview_formatter',
+      'variables' => array('element' => NULL),
+    ),
+
+  );
+
+//  if (module_exists('imagecache')) {
+//    foreach (imagecache_presets() as $preset) {
+//      $theme['pdfpreview_formatter_'. $preset['presetname'] . '][pdfpreview'] = array(
+//        'function' => 'theme_pdfpreview_formatter',
+//        'arguments' => array('element' => NULL),
+//      );
+//    }
+//  }
+
+  return $theme;
+}
+
+
+// ----- custom module functions ----- //
+
+
 /**
- * Creates the settings form to tune how pdfpreview works.
- * @return themed settings form
+ * Creates the module settings render array
+ *
+ * If you want to extend the module's settings form, append the elements you
+ * need to the $form array. Don't forget to update the pdfpreview_uninstall()
+ * routine in the pdfpreview.install file as well.
+ *
  */
-function pdfpreview_admin_settings() {
+function _pdfpreview_admin_settings() {
   $form = array(
       'pdfpreview_pathtoimages' => array(
           '#type' => 'textfield',
@@ -131,6 +135,12 @@ function pdfpreview_admin_settings() {
           '#description' => t('Path, inside files directory, where snapshots are stored. For example <em>pdfpreview</em>'),
           '#default_value' => variable_get('pdfpreview_pathtoimages', 'pdfpreview'),
       ),
+      'pdfpreview_previewsize' => array(
+          '#type' => 'textfield',
+          '#title' => t('Preview size'),
+          '#description' => t('Size of the preview in pixels. For example <em>100x100</em>'),
+          '#default_value' => variable_get('pdfpreview_previewsize', '100x100'),
+      ),
       'pdfpreview_description' => array(
           '#type' => 'checkbox',
           '#title' => t('Description'),
@@ -147,5 +157,137 @@ function pdfpreview_admin_settings() {
       ),
   );
 
+  // Callback function which checks for existing preview directory on save
+  $form['#submit'][] = '_pdfpreview_prepare_filesystem';
+
   return system_settings_form($form);
 }
+
+
+/**
+ * Convert the first page of a PDF document
+ *
+ * This function converts the first page of a PDF document using ImageMagick's
+ * convert executable and returns TRUE on success, FALSE else. You can provide
+ * optional parameters for the convert executable by simply passing them with
+ * in an array to the $args parameter. For details about convert arguments see
+ * the <a href="http://www.imagemagick.org/Usage/basics/#cmdline">ImageMagick
+ * documentation</a>.
+ *
+ * @param $source URI to the PDF file
+ * @param $dest URI where the preview file will be saved
+ * @param $args Optional arguments for the convert executable
+ * @see _imagemagick_convert()
+ * @see _pdfpreview_create_preview()
+ */
+function _pdfpreview_convert_first_page($source, $dest, $args = array()) {
+  $source = drupal_realpath($source).'[0]';
+  $dest = drupal_realpath($dest);
+
+  $args['quality'] = '-quality ' . escapeshellarg(variable_get('imagemagick_quality', 75));
+  $args['previewsize'] = '-resize ' . escapeshellarg(variable_get('pdfpreview_previewsize', '100x100'));
+
+  $context = array(
+    'source' => $source,
+    'destination' => $dest,
+  );
+
+  drupal_alter('imagemagick_arguments', $args, $context);
+  // To make use of ImageMagick 6's parenthetical command grouping we need to make
+  // the $source image the first parameter and $dest the last.
+  // See http://www.imagemagick.org/Usage/basics/#cmdline
+  $command = escapeshellarg($source) . ' ' . implode(' ', $args) . ' ' . escapeshellarg($dest);
+
+  if (_imagemagick_convert_exec($command, $output, $error) !== TRUE) {
+    return FALSE;
+  }
+  return file_exists($dest);
+}
+
+
+/**
+ * Creates the PDF preview file and returns its URI
+ *
+ * @param File $file
+ * @return string URI of the newly created preview image
+ * @see _pdfpreview_convert_first_page()
+ * @see pdfpreview_field_formatter_view()
+ */
+function _pdfpreview_create_preview($file) {
+  $output_dir = file_default_scheme() . '://' . variable_get('pdfpreview_pathtoimages', 'pdfpreview');
+  $output_filename = $output_dir . '/' . md5('pdfpreview' . $file->fid) . '.jpg';
+  // Create preview image using ImageMagick
+  if (!file_exists($output_filename) && function_exists('_imagemagick_convert')) {
+    $pdf = drupal_realpath($file->uri);
+    _pdfpreview_convert_first_page($pdf, $output_filename);
+  }
+  return $output_filename;
+}
+
+
+/**
+ * Prepare the file system for PDF preview image creation
+ *
+ * This function is called after the module's setting form is submitted. It
+ * checks whether the target directory for preview images exists. If this is not
+ * the case, the directory will be created.
+ *
+ * @see _pdfpreview_admin_settings()
+ * @see system_settings_form_submit()
+ */
+function _pdfpreview_prepare_filesystem($form, &$form_state) {
+  $output_uri = file_default_scheme() . '://' . $form['pdfpreview_pathtoimages']['#value'];
+  $output_dir = drupal_realpath($output_uri);
+
+  if (!file_exists($output_dir)) {
+    if (!mkdir($output_dir)) {
+      drupal_set_message(t('Error creating directory @dir', array('%dir' => $output_dir)), 'error');
+      watchdog('pdfpreview', 'Error creating directory @dir', array('%dir' => $output_dir), WATCHDOG_ERROR);
+    }
+    $message = t('The directory %dir has been created', array('%dir' => $output_dir));
+    watchdog('pdfpreview', 'The directory %dir has been created', array('%dir' => $output_dir));
+    drupal_set_message($message, 'status');
+  }
+  elseif (!is_dir($output_dir)) {
+    $message = t('The path %dir is not a directory', array('%dir' => $output_dir));
+    watchdog('pdfpreview', 'The path %dir is not a directory', array('%dir' => $output_dir), WATCHDOG_ERROR);
+    drupal_set_message($message, 'error');
+  }
+}
+
+
+/**
+ * Theming functions for our formatters
+ *
+ * @todo Clearify the image cache thing...
+ * @todo Let user choose what preview image to show on non-PDF files
+ */
+function theme_pdfpreview_formatter($element) {
+  $img_tag = '';
+  $item = $element['item'];
+
+  // TODO: Not sure what this part is doing...
+  // if (list($namespace, $presetname) = explode('][', $element['#formatter'], 2)) {
+  //   if ($preset = imagecache_preset_by_name($namespace)) {
+  //     $output = theme('imagecache', $namespace, $output_filename, $item['data']['alt'], $item['data']['title']);
+  //   }
+  // }
+
+  //debug($item);
+
+  $file_url = file_create_url($item['uri']);
+  $img_url = file_create_url($item['preview']);
+  $title = $alt = $item['description'];
+  $img_tag = sprintf('<img src="%s" title="%s" alt="%s" />', $img_url, $title, $alt);
+  $wrapper_tag = variable_get('pdfpreview_tag', 'span');
+  $description = (variable_get("pdfpreview_description", 1)) ? '<'.$wrapper_tag.' class="pdfpreview-description">'.$title.'</'.$wrapper_tag.'>' : '' ;
+  return sprintf(
+          '<div class="pdfpreview" id="pdfpreview-%s">'
+          .' <%s class="pdfpreview-image-wrapper"><a href="%s" title="%s">%s</a></%s>'
+          .' %s'
+          .'</div>',
+          $item['fid'],
+          $wrapper_tag, $file_url, $title, $img_tag, $wrapper_tag,
+          $description
+  );
+}
