diff --git a/h5p.info.yml b/h5p.info.yml
index ed702b76..b8c83e4b 100644
--- a/h5p.info.yml
+++ b/h5p.info.yml
@@ -3,6 +3,7 @@ type: module
 description: 'Upload and view interactive HTML5 Packages to your site.'
 package: H5P
 core: 8.x
+core_version_requirement: ^8 || ^9
 dependencies:
   - drupal:field
   - drupal:file
diff --git a/h5p.install b/h5p.install
index 32a108ee..7cc7672e 100644
--- a/h5p.install
+++ b/h5p.install
@@ -7,6 +7,7 @@
 use Drupal\h5p\H5PDrupal\H5PDrupal;
 use Drupal\Core\Database\Query\Condition;
 use Drupal\Core\Field\BaseFieldDefinition;
+use Drupal\Core\Render\Markup;
 
 /**
  * Called when uninstalling H5P. Cleans up files and settings.
@@ -54,12 +55,12 @@ function h5p_uninstall() {
   }
 
   // Show link to h5p.org form
-  $survey_message = \Drupal\Core\Render\Markup::create('H5P has now been disabled. Answer the short survey below and get a chance to win a US $500 Amazon Gift Card.</br><iframe src="http://h5p.org/uninstall-form" width="100%" height="360px" style="border: 0;"></iframe>');
-  drupal_set_message($survey_message);
+  $survey_message = Markup::create('H5P has now been disabled. Answer the short survey below and get a chance to win a US $500 Amazon Gift Card.</br><iframe src="http://h5p.org/uninstall-form" width="100%" height="360px" style="border: 0;"></iframe>');
+  \Drupal::messenger()->addStatus($survey_message);
 
   // Alternative text for drush uninstalls
-  $feedback_message = \Drupal\Core\Render\Markup::create('Leaving because the H5P plugin isn’t working for you? Please share your feedback so we can improve. <a href=\"https://h5p.org/drupal-module-disabled\">https://h5p.org/drupal-module-disabled</a>');
-  drupal_set_message($feedback_message);
+  $feedback_message = Markup::create('Leaving because the H5P plugin isn\'t working for you? Please share your feedback so we can improve. <a href=\"https://h5p.org/drupal-module-disabled\">https://h5p.org/drupal-module-disabled</a>');
+  \Drupal::messenger()->addStatus($feedback_message);
 
   // Intentionally not deleting variable h5p_site_uuid. If h5p module is uninstalled/installed,
   // the uuid should be the same
@@ -74,7 +75,7 @@ function h5p_uninstall() {
 function h5p_install() {
 
   // Notify of communication channels
-  drupal_set_message(
+  \Drupal::messenger()->addStatus(
     t('H5P fetches content types directly from the H5P Hub. In order to do this the H5P plugin will communicate with the Hub once a day to fetch information about new and updated content types. It will send in anonymous data to the Hub about H5P usage. Read more at <a href="https://h5p.org/tracking-the-usage-of-h5p">the plugin communication page at H5P.org</a>.')
   );
 
@@ -256,8 +257,9 @@ function _h5p_recursive_unlink($file, $connection) {
     if ($fid !== false) {
 
       // If it was found delete file and database record
-      $file_object = \Drupal::entityTypeManager()->getStorage('file')->resetCache([$fid]);
-      file_delete($file_object);
+      $file_storage = \Drupal::entityTypeManager()->getStorage('file');
+      $file_object = $file_storage->resetCache([$fid]);
+      $file_storage->delete($file_object);
     } else {
 
       // Delete locally
@@ -835,21 +837,22 @@ function h5p_update_8001(&$sandbox) {
     ->clear('h5p_fetched_library_metadata_on')
     ->save();
 
-  drupal_set_message('Module h5p removed configuration items successfully.');
+  \Drupal::messenger()->addStatus('Module h5p removed configuration items successfully.');
 }
 
 /**
  * Add fields for metadata and addons.
  */
 function h5p_update_8002() {
-  if (!db_field_exists('h5p_libraries', 'add_to')) {
-    db_add_field('h5p_libraries', 'add_to', array(
+  $database = \Drupal::service('database');
+  if (!$database->schema()->fieldExists('h5p_libraries', 'add_to')) {
+    $database->schema()->addField('h5p_libraries', 'add_to', array(
       'description' => 'plugin configuration data',
       'type' => 'text'
     ));
   }
-  if (!db_field_exists('h5p_libraries', 'metadata_settings')) {
-    db_add_field('h5p_libraries', 'metadata_settings', array(
+  if (!$database->schema()->fieldExists('h5p_libraries', 'add_to')) {
+    $database->schema()->addField('h5p_libraries', 'metadata_settings', array(
       'description' => 'Metadata settings',
       'type' => 'text'
     ));
@@ -951,7 +954,7 @@ function h5p_update_8002() {
  * Make the content files permanent
  */
 function h5p_update_8003(&$sandbox) {
-  db_update('file_managed')
+  \Drupal::database()->update('file_managed')
     ->fields(array(
       'status' => 1,
     ))
diff --git a/h5p.module b/h5p.module
index 74e3de2f..67918e43 100644
--- a/h5p.module
+++ b/h5p.module
@@ -147,7 +147,7 @@ function h5p_library_info_build() {
   ];*/
 
   // Load installed libraries
-  $h5p_libraries = db_query("SELECT library_id AS id,
+  $h5p_libraries = \Drupal::database()->query("SELECT library_id AS id,
                                 machine_name AS name,
                                 major_version AS major,
                                 minor_version AS minor,
@@ -178,7 +178,7 @@ function h5p_library_info_build() {
     }
 
     // Load dependencies
-    $h5p_dependencies = db_query(
+    $h5p_dependencies = \Drupal::database()->query(
         "SELECT hl.machine_name AS name,
                 hl.major_version AS major,
                 hl.minor_version AS minor
@@ -250,13 +250,13 @@ function h5p_cron() {
  *
  * @see hook_ENTITY_TYPE_delete()
  */
-function h5p_user_delete(Drupal\Core\Entity\EntityInterface $entity) {
-  db_delete('h5p_points')
+function h5p_user_delete(\Drupal\Core\Entity\EntityInterface $entity) {
+  \Drupal::database()->delete('h5p_points')
     ->condition('uid', $entity->id())
     ->execute();
 
   // Remove content user data
-  db_delete('h5p_content_user_data')
+  \Drupal::database()->delete('h5p_content_user_data')
     ->condition('user_id', $entity->id())
     ->execute();
 }
diff --git a/modules/h5peditor/h5peditor.info.yml b/modules/h5peditor/h5peditor.info.yml
index 785a892f..fc871145 100644
--- a/modules/h5peditor/h5peditor.info.yml
+++ b/modules/h5peditor/h5peditor.info.yml
@@ -3,5 +3,6 @@ type: module
 description: 'Create and modify interactive HTML5 packages on your site.'
 package: H5P
 core: 8.x
+core_version_requirement: ^8 || ^9
 dependencies:
   - h5p:h5p
diff --git a/modules/h5peditor/src/H5PEditor/H5PEditorDrupalAjax.php b/modules/h5peditor/src/H5PEditor/H5PEditorDrupalAjax.php
index a2da431e..390401ef 100644
--- a/modules/h5peditor/src/H5PEditor/H5PEditorDrupalAjax.php
+++ b/modules/h5peditor/src/H5PEditor/H5PEditorDrupalAjax.php
@@ -74,7 +74,7 @@ class H5PEditorDrupalAjax implements \H5PEditorAjaxInterface {
 
     // Get only the specified content type from cache
     if ($machineName !== NULL) {
-      return db_query(
+      return \Drupal::database()->query(
         "SELECT id, is_recommended
          FROM {h5p_libraries_hub_cache}
         WHERE machine_name = :name",
@@ -83,7 +83,7 @@ class H5PEditorDrupalAjax implements \H5PEditorAjaxInterface {
     }
 
     // Get all cached content types
-    return db_query("SELECT * FROM {h5p_libraries_hub_cache}")->fetchAll();
+    return \Drupal::database()->query("SELECT * FROM {h5p_libraries_hub_cache}")->fetchAll();
   }
 
   /**
@@ -99,7 +99,7 @@ class H5PEditorDrupalAjax implements \H5PEditorAjaxInterface {
     $recently_used = array();
 
     // Get recently used:
-    $result = db_query("
+    $result = \Drupal::database()->query("
       SELECT library_name, max(created_at) AS max_created_at
       FROM {h5p_events}
       WHERE type='content' AND sub_type = 'create' AND user_id = :uid
@@ -138,7 +138,7 @@ class H5PEditorDrupalAjax implements \H5PEditorAjaxInterface {
     foreach ($libraries as $library) {
       $parsedLib = \H5PCore::libraryFromString($library);
 
-      $translation = db_query("
+      $translation = \Drupal::database()->query("
         SELECT language_json
         FROM {h5p_libraries} lib
         LEFT JOIN {h5p_libraries_languages} lang ON lib.library_id = lang.library_id
@@ -146,13 +146,11 @@ class H5PEditorDrupalAjax implements \H5PEditorAjaxInterface {
               lib.major_version = :major_version AND
               lib.minor_version = :minor_version AND
               lang.language_code = :language_code
-      ",
-      array(
+      ", array(
         ':machine_name' => $parsedLib['machineName'],
         ':major_version' => $parsedLib['majorVersion'],
         ':minor_version' => $parsedLib['minorVersion'],
-        ':language_code' => $language_code)
-      )->fetchField();
+        ':language_code' => $language_code))->fetchField();
 
       if ($translation !== FALSE) {
         $translations[$library] = $translation;
diff --git a/modules/h5peditor/src/H5PEditor/H5PEditorDrupalStorage.php b/modules/h5peditor/src/H5PEditor/H5PEditorDrupalStorage.php
index 9060b920..99deea04 100644
--- a/modules/h5peditor/src/H5PEditor/H5PEditorDrupalStorage.php
+++ b/modules/h5peditor/src/H5PEditor/H5PEditorDrupalStorage.php
@@ -2,6 +2,7 @@
 
 namespace Drupal\h5peditor\H5PEditor;
 
+use Drupal\Core\File\FileSystemInterface;
 use Drupal\file\Entity\File;
 use Drupal\h5p\H5PDrupal\H5PDrupal;
 
@@ -18,21 +19,19 @@ class H5PEditorDrupalStorage implements \H5peditorStorage {
    * @return string Translation in JSON format
    */
   public function getLanguage($machineName, $majorVersion, $minorVersion, $languageCode) {
-    $lang = db_query(
-      "SELECT language_json
+    $lang = \Drupal::database()->query("SELECT language_json
            FROM {h5p_libraries_languages} hlt
            JOIN {h5p_libraries} hl
              ON hl.library_id = hlt.library_id
           WHERE hl.machine_name = :name
             AND hl.major_version = :major
             AND hl.minor_version = :minor
-            AND hlt.language_code = :lang",
-      array(
-        ':name' => $machineName,
-        ':major' => $majorVersion,
-        ':minor' => $minorVersion,
-        ':lang' => $languageCode,
-      ))->fetchField();
+            AND hlt.language_code = :lang", array(
+      ':name' => $machineName,
+      ':major' => $majorVersion,
+      ':minor' => $minorVersion,
+      ':lang' => $languageCode,
+    ))->fetchField();
 
     return ($lang === FALSE ? NULL : $lang);
   }
@@ -46,19 +45,17 @@ class H5PEditorDrupalStorage implements \H5peditorStorage {
    * @return array List of possible language codes
    */
   public function getAvailableLanguages($machineName, $majorVersion, $minorVersion) {
-    $results = db_query(
-        "SELECT language_code
+    $results = \Drupal::database()->query("SELECT language_code
            FROM {h5p_libraries_languages} hlt
            JOIN {h5p_libraries} hl
              ON hl.library_id = hlt.library_id
           WHERE hl.machine_name = :name
             AND hl.major_version = :major
-            AND hl.minor_version = :minor",
-        array(
-          ':name' => $machineName,
-          ':major' => $majorVersion,
-          ':minor' => $minorVersion
-        ));
+            AND hl.minor_version = :minor", array(
+      ':name' => $machineName,
+      ':major' => $majorVersion,
+      ':minor' => $minorVersion
+    ));
 
     $codes = array('en'); // Semantics is 'en' by default.
     foreach ($results as $result) {
@@ -108,18 +105,20 @@ class H5PEditorDrupalStorage implements \H5peditorStorage {
       // Get details for the specified libraries only.
       $librariesWithDetails = array();
       foreach ($libraries as $library) {
-        $details = db_query(
-          "SELECT title, runnable, restricted, tutorial_url, metadata_settings
+        $details = \Drupal::database()->query(
+            "SELECT title, runnable, restricted, tutorial_url, metadata_settings
            FROM {h5p_libraries}
            WHERE machine_name = :name
            AND major_version = :major
            AND minor_version = :minor
-           AND semantics IS NOT NULL", // TODO: Consider if semantics is really needed (DB performance-wise)
-          array(
-            ':name' => $library->name,
-            ':major' => $library->majorVersion,
-            ':minor' => $library->minorVersion
-          ))
+           AND semantics IS NOT NULL",
+            // TODO: Consider if semantics is really needed (DB performance-wise)
+            array(
+              ':name' => $library->name,
+              ':major' => $library->majorVersion,
+              ':minor' => $library->minorVersion
+            )
+        )
           ->fetchObject();
         if ($details !== FALSE) {
           $library->tutorialUrl = $details->tutorial_url;
@@ -136,8 +135,7 @@ class H5PEditorDrupalStorage implements \H5peditorStorage {
 
     $libraries = array();
 
-    $libraries_result = db_query(
-      "SELECT machine_name AS name,
+    $libraries_result = \Drupal::database()->query("SELECT machine_name AS name,
               title,
               major_version,
               minor_version,
@@ -219,14 +217,14 @@ class H5PEditorDrupalStorage implements \H5peditorStorage {
     $temp_id = uniqid('h5p-');
 
     $temporary_file_path = "public://{$h5p_path}/temp/{$temp_id}";
-    file_prepare_directory($temporary_file_path, FILE_CREATE_DIRECTORY);
+    \Drupal::service('file_system')->prepareDirectory($temporary_file_path, FileSystemInterface::CREATE_DIRECTORY);
     $name = $temp_id . '.h5p';
     $target = $temporary_file_path . DIRECTORY_SEPARATOR . $name;
     if ($move_file) {
       $file = move_uploaded_file($data, $target);
     }
     else {
-      $file = file_unmanaged_save_data($data, $target);
+      $file = \Drupal::service('file_system')->saveData($data, $target);
     }
     if (!$file) {
       return FALSE;
diff --git a/src/Controller/H5PAJAX.php b/src/Controller/H5PAJAX.php
index 0ad43e8f..6cf21bb3 100644
--- a/src/Controller/H5PAJAX.php
+++ b/src/Controller/H5PAJAX.php
@@ -2,12 +2,11 @@
 
 namespace Drupal\h5p\Controller;
 
-use Drupal\Core\Url;
+use Drupal\Core\Cache\Cache;
 use Drupal\Core\Controller\ControllerBase;
 use Drupal\Core\Database\Connection;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 use Symfony\Component\HttpFoundation\JsonResponse;
-use Drupal\h5p\H5PDrupal\H5PDrupal;
 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
 use Drupal\h5p\Event\FinishedEvent;
 
@@ -128,7 +127,7 @@ class H5PAJAX extends ControllerBase {
 
       if ($data === '0') {
         // Remove data
-        db_delete('h5p_content_user_data')
+        \Drupal::database()->delete('h5p_content_user_data')
           ->condition('content_main_id', $content_main_id)
           ->condition('data_id', $data_id)
           ->condition('user_id', $user->id())
@@ -140,22 +139,21 @@ class H5PAJAX extends ControllerBase {
         $invalidate = ($invalidate === '0' ? 0 : 1);
 
         // Determine if we should update or insert
-        $update = db_query("SELECT content_main_id
+        $update = \Drupal::database()->query("SELECT content_main_id
                                    FROM {h5p_content_user_data}
                                    WHERE content_main_id = :content_main_id
                                      AND user_id = :user_id
                                      AND data_id = :data_id
-                                     AND sub_content_id = :sub_content_id",
-          array(
-            ':content_main_id' => $content_main_id,
-            ':user_id' => $user->id(),
-            ':data_id' => $data_id,
-            ':sub_content_id' => $sub_content_id,
-          ))->fetchField();
+                                     AND sub_content_id = :sub_content_id", array(
+          ':content_main_id' => $content_main_id,
+          ':user_id' => $user->id(),
+          ':data_id' => $data_id,
+          ':sub_content_id' => $sub_content_id,
+        ))->fetchField();
 
         if ($update === FALSE) {
           // Insert new data
-          db_insert('h5p_content_user_data')
+          \Drupal::database()->insert('h5p_content_user_data')
             ->fields(array(
               'user_id' => $user->id(),
               'content_main_id' => $content_main_id,
@@ -170,7 +168,7 @@ class H5PAJAX extends ControllerBase {
         }
         else {
           // Update old data
-          db_update('h5p_content_user_data')
+          \Drupal::database()->update('h5p_content_user_data')
             ->fields(array(
               'timestamp' => time(),
               'data' => $data,
@@ -185,21 +183,20 @@ class H5PAJAX extends ControllerBase {
         }
       }
 
-      \Drupal\Core\Cache\Cache::invalidateTags(['h5p_content:' . $content_main_id]);
+      Cache::invalidateTags(['h5p_content:' . $content_main_id]);
       return new JsonResponse($response);
     } else {
       // Fetch data
-      $response->data = db_query("SELECT data FROM {h5p_content_user_data}
+      $response->data = \Drupal::database()->query("SELECT data FROM {h5p_content_user_data}
                                 WHERE user_id = :user_id
                                   AND content_main_id = :content_main_id
                                   AND data_id = :data_id
-                                  AND sub_content_id = :sub_content_id",
-        array(
-          ':user_id' => $user->id(),
-          ':content_main_id' => $content_main_id,
-          ':sub_content_id' => $sub_content_id,
-          ':data_id' => $data_id,
-        ))->fetchField();
+                                  AND sub_content_id = :sub_content_id", array(
+        ':user_id' => $user->id(),
+        ':content_main_id' => $content_main_id,
+        ':sub_content_id' => $sub_content_id,
+        ':data_id' => $data_id,
+      ))->fetchField();
     }
 
     return new JsonResponse($response);
diff --git a/src/Entity/H5PContent.php b/src/Entity/H5PContent.php
index bb840c57..394e0b81 100644
--- a/src/Entity/H5PContent.php
+++ b/src/Entity/H5PContent.php
@@ -136,24 +136,19 @@ class H5PContent extends ContentEntityBase implements ContentEntityInterface {
    * Load library used by content
    */
   protected function loadLibrary() {
-    $this->library = db_query(
-        "SELECT  title,
+    $this->library = \Drupal::database()->query("SELECT  title,
                  machine_name AS name,
                  major_version AS major,
                  minor_version AS minor,
                  embed_types,
                  fullscreen
             FROM {h5p_libraries}
-           WHERE library_id = :id",
-        [
-          ':id' => $this->get('library_id')->value
-        ])
+           WHERE library_id = :id", [
+      ':id' => $this->get('library_id')->value
+    ])
         ->fetchObject();
   }
 
-  /**
-   *
-   */
   public function getLibrary($assoc = FALSE) {
     if (empty($this->library)) {
       $this->loadLibrary();
@@ -171,9 +166,6 @@ class H5PContent extends ContentEntityBase implements ContentEntityInterface {
     return $this->library;
   }
 
-  /**
-   *
-   */
   public function getLibraryString() {
     if (empty($this->library)) {
       $this->loadLibrary();
@@ -182,16 +174,10 @@ class H5PContent extends ContentEntityBase implements ContentEntityInterface {
     return "{$this->library->name} {$this->library->major}.{$this->library->minor}";
   }
 
-  /**
-   *
-   */
   public function getLibraryId() {
     return $this->get('library_id')->value;
   }
 
-  /**
-   *
-   */
   public function isDivEmbeddable() {
     if (empty($this->library)) {
       $this->loadLibrary();
@@ -200,9 +186,6 @@ class H5PContent extends ContentEntityBase implements ContentEntityInterface {
     return (strpos($this->library->embed_types, 'iframe') === FALSE);
   }
 
-  /**
-   *
-   */
   protected function getExportURL() {
     $interface = H5PDrupal::getInstance();
     if (empty($interface->getOption('export', TRUE))) {
@@ -220,9 +203,6 @@ class H5PContent extends ContentEntityBase implements ContentEntityInterface {
     return json_decode($this->get('parameters')->value);
   }
 
-  /**
-   *
-   */
   public function getFilteredParameters() {
     if (empty($this->library)) {
       $this->loadLibrary();
@@ -292,26 +272,20 @@ class H5PContent extends ContentEntityBase implements ContentEntityInterface {
     return '{"params":' . $this->getFilteredParameters() . ',"metadata":' . json_encode($this->getMetadata()) . '}';
   }
 
-  /**
-   *
-   */
   public function getH5PIntegrationSettings($canUpdateEntity = FALSE) {
     if (empty($this->library)) {
       $this->loadLibrary();
     }
 
     // Load user data for content
-    $results = db_query(
-        "SELECT sub_content_id, data_id, data
+    $results = \Drupal::database()->query("SELECT sub_content_id, data_id, data
            FROM {h5p_content_user_data}
           WHERE user_id = :user_id
             AND content_main_id = :content_id
-            AND preloaded = 1",
-        [
-          ':user_id' => \Drupal::currentUser()->id(),
-          ':content_id' => $this->id(),
-        ]
-    );
+            AND preloaded = 1", [
+      ':user_id' => \Drupal::currentUser()->id(),
+      ':content_id' => $this->id(),
+    ]);
 
     $content_user_data = [
       0 => [
diff --git a/src/Event/FinishedEvent.php b/src/Event/FinishedEvent.php
index 43eba57c..56a147d9 100644
--- a/src/Event/FinishedEvent.php
+++ b/src/Event/FinishedEvent.php
@@ -2,8 +2,7 @@
 
 namespace Drupal\h5p\Event;
 
-use Symfony\Component\EventDispatcher\Event;
-
+use Symfony\Contracts\EventDispatcher\Event;
 /**
  * Wraps a node insertion demo event for event listeners.
  */
diff --git a/src/Form/H5PAdminSettingsForm.php b/src/Form/H5PAdminSettingsForm.php
index c6b54bea..f18e0d93 100644
--- a/src/Form/H5PAdminSettingsForm.php
+++ b/src/Form/H5PAdminSettingsForm.php
@@ -2,6 +2,7 @@
 
 namespace Drupal\h5p\Form;
 
+use Drupal\Core\File\FileSystemInterface;
 use Drupal\h5p\H5PDrupal\H5PDrupal;
 
 use Drupal\Core\Form\FormBase;
@@ -204,10 +205,10 @@ class H5PAdminSettingsForm extends FormBase {
     $path = \Drupal::service('file_system')->realpath('public://') . '/' . $h5p_default_path;
     $temp_path = $path . '/' . 'temp';
 
-    if (! file_prepare_directory($path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
+    if (! \Drupal::service('file_system')->prepareDirectory($path, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS)) {
       $form_state->setErrorByName('h5p_default_path', t('You have specified an invalid directory.'));
     }
-    if (! file_prepare_directory($temp_path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
+    if (! \Drupal::service('file_system')->prepareDirectory($temp_path, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS)) {
       $form_state->setErrorByName('h5p_default_path', t('You have specified an invalid directory.'));
     }
 
diff --git a/src/Form/H5PLibraryUploadForm.php b/src/Form/H5PLibraryUploadForm.php
index 807cbf97..58935c0b 100644
--- a/src/Form/H5PLibraryUploadForm.php
+++ b/src/Form/H5PLibraryUploadForm.php
@@ -2,6 +2,7 @@
 
 namespace Drupal\h5p\Form;
 
+use Drupal\Core\File\FileSystemInterface;
 use Drupal\h5p\H5PDrupal\H5PDrupal;
 use Drupal\h5p\H5PApi\H5PClasses;
 use Drupal\h5p\H5PApi\H5PFileStorageInterface;
@@ -72,7 +73,7 @@ class H5PLibraryUploadForm extends FormBase {
 
     $h5p_default_path = $interface->getOption('default_path', 'h5p');
     $temporary_file_path = 'public://' . $h5p_default_path . '/temp/' . uniqid('h5p-');
-    file_prepare_directory($temporary_file_path, FILE_CREATE_DIRECTORY);
+    \Drupal::service('file_system')->prepareDirectory($temporary_file_path, FileSystemInterface::CREATE_DIRECTORY);
 
     $file = file_save_upload('h5p', $validators, $temporary_file_path);
     if (sizeof($file) !== 0 && $file[0] !== FALSE) {
diff --git a/src/H5PDrupal/H5PDrupal.php b/src/H5PDrupal/H5PDrupal.php
index dfc95974..81ee4708 100644
--- a/src/H5PDrupal/H5PDrupal.php
+++ b/src/H5PDrupal/H5PDrupal.php
@@ -2,11 +2,11 @@
 
 namespace Drupal\h5p\H5PDrupal;
 
+use Drupal\h5peditor\H5PEditor\H5PEditorDrupalStorage;
+use Drupal\Core\Cache\Cache;
 use Drupal\h5p\Entity\H5PContent;
-use Drupal\h5peditor\H5PEditor;
 use Drupal\Core\Url;
 use Drupal\Core\StreamWrapper\PublicStream;
-use Drupal\Component\Utility\UrlHelper;
 
 class H5PDrupal implements \H5PFrameworkInterface {
   private $h5pPath, $folderPath;
@@ -53,7 +53,7 @@ class H5PDrupal implements \H5PFrameworkInterface {
     }
     else {
       // Get runtime cache
-      list($interface, $core) = $instances[$instance];
+      [$interface, $core] = $instances[$instance];
     }
 
     switch ($type) {
@@ -175,9 +175,6 @@ class H5PDrupal implements \H5PFrameworkInterface {
     return $assets;
   }
 
-  /**
-   *
-   */
   public static function aggregatedAssets($scriptAssets, $styleAssets) {
     $jsOptimizer = \Drupal::service('asset.js.collection_optimizer');
     $cssOptimizer = \Drupal::service('asset.css.collection_optimizer');
@@ -198,7 +195,7 @@ class H5PDrupal implements \H5PFrameworkInterface {
    * Combines a set of files to a cached version, that is public available
    *
    * @param string[] $filePaths
-   * @param AssetCollectionOptimizerInterface $optimizer
+   * @param $optimizer
    * @param array $assetConfig
    *
    * @return string[]
@@ -234,7 +231,7 @@ class H5PDrupal implements \H5PFrameworkInterface {
   public function removeOldLogEvents() {
     $older_than = (time() - \H5PEventBase::$log_time);
 
-    db_delete('h5p_events')
+    \Drupal::database()->delete('h5p_events')
       ->condition('created_at', $older_than, '<')
       ->execute();
   }
@@ -262,7 +259,7 @@ class H5PDrupal implements \H5PFrameworkInterface {
    */
   public function getPlatformInfo() {
 
-    $h5p_info = system_get_info('module', 'h5p');
+    $h5p_info = \Drupal::service('extension.list.module')->getExtensionInfo('h5p');
 
     return [
       'name' => 'drupal',
@@ -304,8 +301,8 @@ class H5PDrupal implements \H5PFrameworkInterface {
 
     if ($stream && empty($response->error)) {
       // Create file from data
-      H5PEditor\H5peditorDrupalStorage::saveFileTemporarily($response_data);
-      // TODO: Cannot rely on H5PEditor module – Perhaps we could use the
+      H5PEditorDrupalStorage::saveFileTemporarily($response_data);
+      // TODO: Cannot rely on H5PEditor module - Perhaps we could use the
       // save_to/sink option to save directly to file when streaming ?
       // http://guzzle.readthedocs.io/en/latest/request-options.html#sink-option
       return TRUE;
@@ -323,7 +320,7 @@ class H5PDrupal implements \H5PFrameworkInterface {
    * @param string $tutorialUrl
    */
   public function setLibraryTutorialUrl($machineName, $tutorialUrl) {
-    db_update('h5p_libraries')
+    \Drupal::database()->update('h5p_libraries')
       ->fields([
         'tutorial_url' => $tutorialUrl,
       ])
@@ -345,7 +342,7 @@ class H5PDrupal implements \H5PFrameworkInterface {
       'code' => $code,
       'message' => $message
     );
-    drupal_set_message($message, 'error');
+    \Drupal::messenger()->addError($message);
   }
 
   /**
@@ -353,7 +350,7 @@ class H5PDrupal implements \H5PFrameworkInterface {
    */
   public function setInfoMessage($message) {
     $this->messages['info'][] = $message;
-    drupal_set_message($message);
+    \Drupal::messenger()->addStatus($message);
   }
 
   /**
@@ -365,7 +362,7 @@ class H5PDrupal implements \H5PFrameworkInterface {
     }
     $messages = $this->messages[$type];
     $this->messages[$type] = array();
-    drupal_get_messages($type === 'info' ? 'status' : $type, TRUE); // Prevent messages from displaying twice
+    \Drupal::messenger()->messagesByType($type === 'info' ? 'status' : $type); // Prevent messages from displaying twice
     return $messages;
   }
 
@@ -410,8 +407,7 @@ class H5PDrupal implements \H5PFrameworkInterface {
    * Implements loadLibraries
    */
   public function loadLibraries() {
-    $res = db_query(
-        "SELECT library_id AS id,
+    $res = \Drupal::database()->query("SELECT library_id AS id,
                 machine_name AS name,
                 title,
                 major_version, minor_version, patch_version,
@@ -419,8 +415,7 @@ class H5PDrupal implements \H5PFrameworkInterface {
            FROM {h5p_libraries}
        ORDER BY title ASC,
                 major_version ASC,
-                minor_version ASC"
-    );
+                minor_version ASC");
 
     $libraries = [];
     foreach ($res as $library) {
@@ -443,18 +438,15 @@ class H5PDrupal implements \H5PFrameworkInterface {
    * Implements getLibraryId
    */
   public function getLibraryId($machineName, $majorVersion = NULL, $minorVersion = NULL) {
-    $library_id = db_query(
-        "SELECT library_id
+    $library_id = \Drupal::database()->query("SELECT library_id
            FROM {h5p_libraries}
           WHERE machine_name = :machine_name
             AND major_version = :major_version
-            AND minor_version = :minor_version",
-        [
-          ':machine_name' => $machineName,
-          ':major_version' => $majorVersion,
-          ':minor_version' => $minorVersion
-        ]
-    )->fetchField();
+            AND minor_version = :minor_version", [
+      ':machine_name' => $machineName,
+      ':major_version' => $majorVersion,
+      ':minor_version' => $minorVersion
+    ])->fetchField();
 
     return $library_id;
   }
@@ -467,8 +459,7 @@ class H5PDrupal implements \H5PFrameworkInterface {
       return TRUE;
     }
 
-    $result = db_query(
-        "SELECT 1
+    $result = \Drupal::database()->query("SELECT 1
            FROM {h5p_libraries}
           WHERE machine_name = :machineName
             AND major_version = :majorVersion
@@ -520,21 +511,18 @@ class H5PDrupal implements \H5PFrameworkInterface {
       $usage['content'] = -1;
     }
     else {
-      $usage['content'] = intval(db_query(
-          "SELECT COUNT(distinct nfd.id)
+      $usage['content'] = intval(\Drupal::database()->query("SELECT COUNT(distinct nfd.id)
              FROM {h5p_libraries} l
              JOIN {h5p_content_libraries} nl
                ON l.library_id = nl.library_id
              JOIN {h5p_content} nfd
                ON nl.content_id = nfd.id
-            WHERE l.library_id = :id",
-          [
-            ':id' => $libraryId
-          ]
-      )->fetchField());
+            WHERE l.library_id = :id", [
+        ':id' => $libraryId
+      ])->fetchField());
     }
 
-    $usage['libraries'] = intval(db_query(
+    $usage['libraries'] = intval(\Drupal::database()->query(
         "SELECT COUNT(*)
            FROM {h5p_libraries_libraries}
           WHERE required_library_id = :id",
@@ -558,7 +546,7 @@ class H5PDrupal implements \H5PFrameworkInterface {
     $contentCount = [];
 
     // Count content with same machine name, major and minor version
-    $results = db_query(
+    $results = \Drupal::database()->query(
         "SELECT l.machine_name AS name,
                 l.major_version AS major,
                 l.minor_version AS minor,
@@ -568,8 +556,7 @@ class H5PDrupal implements \H5PFrameworkInterface {
           WHERE c.library_id = l.library_id
        GROUP BY l.machine_name,
                 l.major_version,
-                l.minor_version"
-    );
+                l.minor_version");
 
     // Format results
     foreach($results as $library) {
@@ -585,7 +572,7 @@ class H5PDrupal implements \H5PFrameworkInterface {
   public function getLibraryStats($type) {
     $count = [];
 
-    $results = db_query(
+    $results = \Drupal::database()->query(
         "SELECT library_name AS name,
                 library_version AS version,
                 num
@@ -609,7 +596,7 @@ class H5PDrupal implements \H5PFrameworkInterface {
    */
   public function getNumAuthors() {
 
-    $id = db_query(
+    $id = \Drupal::database()->query(
         "SELECT id
            FROM {h5p_content}
           LIMIT 1")->fetchField();
@@ -652,7 +639,7 @@ class H5PDrupal implements \H5PFrameworkInterface {
     }
 
     if ($new) {
-      $libraryId = db_insert('h5p_libraries')
+      $libraryId = \Drupal::database()->insert('h5p_libraries')
         ->fields(array(
           'machine_name' => $libraryData['machineName'],
           'title' => $libraryData['title'],
@@ -680,7 +667,7 @@ class H5PDrupal implements \H5PFrameworkInterface {
       }
     }
     else {
-      db_update('h5p_libraries')
+      \Drupal::database()->update('h5p_libraries')
         ->fields(array(
           'title' => $libraryData['title'],
           'patch_version' => $libraryData['patchVersion'],
@@ -710,12 +697,12 @@ class H5PDrupal implements \H5PFrameworkInterface {
     // invoke library installed
     \Drupal::moduleHandler()->invokeAll('h5p_library_installed', array($libraryData, $new));
 
-    db_delete('h5p_libraries_languages')
+    \Drupal::database()->delete('h5p_libraries_languages')
       ->condition('library_id', $libraryData['libraryId'])
       ->execute();
     if (isset($libraryData['language'])) {
       foreach ($libraryData['language'] as $languageCode => $languageJson) {
-        $id = db_insert('h5p_libraries_languages')
+        $id = \Drupal::database()->insert('h5p_libraries_languages')
           ->fields(array(
             'library_id' => $libraryData['libraryId'],
             'language_code' => $languageCode,
@@ -749,17 +736,19 @@ class H5PDrupal implements \H5PFrameworkInterface {
   }
 
   public function lockDependencyStorage() {
-    if (db_driver() === 'mysql') {
+    $database = \Drupal::service('database');
+    if (($database->driver() === 'mysql')) {
       // Only works for mysql, other DBs will have to use transactions.
 
       // db_transaction often deadlocks, we do it more brutally...
-      db_query('LOCK TABLES {h5p_libraries_libraries} write, {h5p_libraries} as hl read');
+      $database->query('LOCK TABLES {h5p_libraries_libraries} write, {h5p_libraries} as hl read');
     }
   }
 
   public function unlockDependencyStorage() {
-    if (db_driver() === 'mysql') {
-      db_query('UNLOCK TABLES');
+    $database = \Drupal::service('database');
+    if (($database->driver() === 'mysql')) {
+      $database->query('UNLOCK TABLES');
     }
   }
 
@@ -767,7 +756,7 @@ class H5PDrupal implements \H5PFrameworkInterface {
    * Implements deleteLibraryDependencies
    */
   public function deleteLibraryDependencies($libraryId) {
-    db_delete('h5p_libraries_libraries')
+    \Drupal::database()->delete('h5p_libraries_libraries')
       ->condition('library_id', $libraryId)
       ->execute();
   }
@@ -776,22 +765,19 @@ class H5PDrupal implements \H5PFrameworkInterface {
    * Implements deleteLibrary. Will delete a library's data both in the database and file system
    */
   public function deleteLibrary($libraryId) {
-    $library = db_query(
-        "SELECT *
+    $library = \Drupal::database()->query("SELECT *
            FROM {h5p_libraries}
-          WHERE library_id = :id",
-        [
-          ':id' => $libraryId
-        ]
-    )->fetchObject();
+          WHERE library_id = :id", [
+      ':id' => $libraryId
+    ])->fetchObject();
 
     // Delete files
     \H5PCore::deleteFileTree(self::getRelativeH5PPath() . "/libraries/{$library->machine_name}-{$library->major_version}.{$library->minor_version}");
 
     // Delete data in database (won't delete content)
-    db_delete('h5p_libraries_libraries')->condition('library_id', $libraryId)->execute();
-    db_delete('h5p_libraries_languages')->condition('library_id', $libraryId)->execute();
-    db_delete('h5p_libraries')->condition('library_id', $libraryId)->execute();
+    \Drupal::database()->delete('h5p_libraries_libraries')->condition('library_id', $libraryId)->execute();
+    \Drupal::database()->delete('h5p_libraries_languages')->condition('library_id', $libraryId)->execute();
+    \Drupal::database()->delete('h5p_libraries')->condition('library_id', $libraryId)->execute();
   }
 
   /**
@@ -799,7 +785,7 @@ class H5PDrupal implements \H5PFrameworkInterface {
    */
   public function saveLibraryDependencies($libraryId, $dependencies, $dependency_type) {
     foreach ($dependencies as $dependency) {
-      $query = db_select('h5p_libraries', 'hl');
+      $query = \Drupal::database()->select('h5p_libraries', 'hl');
       $query->addExpression($libraryId);
       $query->addField('hl', 'library_id');
       $query->addExpression("'{$dependency_type}'");
@@ -807,7 +793,7 @@ class H5PDrupal implements \H5PFrameworkInterface {
       $query->condition('major_version', $dependency['majorVersion']);
       $query->condition('minor_version', $dependency['minorVersion']);
 
-      db_insert('h5p_libraries_libraries')
+      \Drupal::database()->insert('h5p_libraries_libraries')
         /*
          * TODO: The order of the required_library_id and library_id below is reversed,
          * to match the order of the fields in the select statement. We should rather
@@ -893,7 +879,7 @@ class H5PDrupal implements \H5PFrameworkInterface {
    */
   public function resetContentUserData($contentId) {
     // Reset user datas for this content
-    db_update('h5p_content_user_data')
+    \Drupal::database()->update('h5p_content_user_data')
       ->fields(array(
         'timestamp' => time(),
         'data' => 'RESET'
@@ -922,17 +908,14 @@ class H5PDrupal implements \H5PFrameworkInterface {
    * Implements copyLibraryUsage
    */
   public function copyLibraryUsage($contentId, $copyFromId, $contentMainId = NULL) {
-    db_query(
-        "INSERT INTO {h5p_content_libraries}
+    \Drupal::database()->query("INSERT INTO {h5p_content_libraries}
                      (content_id, library_id, dependency_type, drop_css, weight)
               SELECT :toId, hnl.library_id, hnl.dependency_type, hnl.drop_css, hnl.weight
                 FROM {h5p_content_libraries} hnl
-               WHERE hnl.content_id = :fromId",
-        [
-          ':toId' => $contentId,
-          ':fromId' => $copyFromId
-        ]
-    );
+               WHERE hnl.content_id = :fromId", [
+      ':toId' => $contentId,
+      ':fromId' => $copyFromId
+    ]);
   }
 
   /**
@@ -943,12 +926,12 @@ class H5PDrupal implements \H5PFrameworkInterface {
     $this->deleteLibraryUsage($contentId);
 
     // Remove content points
-    db_delete('h5p_points')
+    \Drupal::database()->delete('h5p_points')
       ->condition('content_id', $contentId)
       ->execute();
 
     // Remove content user data
-    db_delete('h5p_content_user_data')
+    \Drupal::database()->delete('h5p_content_user_data')
       ->condition('content_main_id', $contentId)
       ->execute();
   }
@@ -957,7 +940,7 @@ class H5PDrupal implements \H5PFrameworkInterface {
    * Implements deleteLibraryUsage
    */
   public function deleteLibraryUsage($contentId) {
-    db_delete('h5p_content_libraries')
+    \Drupal::database()->delete('h5p_content_libraries')
       ->condition('content_id', $contentId)
       ->execute();
   }
@@ -974,7 +957,7 @@ class H5PDrupal implements \H5PFrameworkInterface {
     }
     foreach ($librariesInUse as $dependency) {
       $dropCss = in_array($dependency['library']['machineName'], $dropLibraryCssList) ? 1 : 0;
-      db_insert('h5p_content_libraries')
+      \Drupal::database()->insert('h5p_content_libraries')
         ->fields(array(
           'content_id' => $contentId,
           'library_id' => $dependency['library']['libraryId'],
@@ -990,8 +973,7 @@ class H5PDrupal implements \H5PFrameworkInterface {
    * Implements loadLibrary
    */
   public function loadLibrary($machineName, $majorVersion, $minorVersion) {
-    $library = db_query(
-        "SELECT library_id,
+    $library = \Drupal::database()->query("SELECT library_id,
                 machine_name,
                 title,
                 major_version,
@@ -1009,13 +991,11 @@ class H5PDrupal implements \H5PFrameworkInterface {
           FROM {h5p_libraries}
           WHERE machine_name = :machine_name
           AND major_version = :major_version
-          AND minor_version = :minor_version",
-        [
-          ':machine_name' => $machineName,
-          ':major_version' => $majorVersion,
-          ':minor_version' => $minorVersion
-        ]
-    )->fetchObject();
+          AND minor_version = :minor_version", [
+      ':machine_name' => $machineName,
+      ':major_version' => $majorVersion,
+      ':minor_version' => $minorVersion
+    ])->fetchObject();
 
     if ($library === FALSE) {
       return FALSE;
@@ -1023,7 +1003,7 @@ class H5PDrupal implements \H5PFrameworkInterface {
     $library = \H5PCore::snakeToCamel($library);
 
     // Load dependencies
-    $result = db_query(
+    $result = \Drupal::database()->query(
         "SELECT hl.machine_name AS name,
                 hl.major_version AS major,
                 hl.minor_version AS minor,
@@ -1052,18 +1032,15 @@ class H5PDrupal implements \H5PFrameworkInterface {
    * Implements loadLibrarySemantics().
    */
   public function loadLibrarySemantics($machineName, $majorVersion, $minorVersion) {
-    $semantics = db_query(
-        "SELECT semantics
+    $semantics = \Drupal::database()->query("SELECT semantics
            FROM {h5p_libraries}
           WHERE machine_name = :machine_name
             AND major_version = :major_version
-            AND minor_version = :minor_version",
-        [
-          ':machine_name' => $machineName,
-          ':major_version' => $majorVersion,
-          ':minor_version' => $minorVersion
-        ]
-    )->fetchField();
+            AND minor_version = :minor_version", [
+      ':machine_name' => $machineName,
+      ':major_version' => $majorVersion,
+      ':minor_version' => $minorVersion
+    ])->fetchField();
 
     return ($semantics === FALSE ? NULL : $semantics);
   }
@@ -1106,7 +1083,7 @@ class H5PDrupal implements \H5PFrameworkInterface {
       $queryArgs[':dt'] = $type;
     }
     $query .= " ORDER BY hnl.weight";
-    $result = db_query($query, $queryArgs);
+    $result = \Drupal::database()->query($query, $queryArgs);
 
     $dependencies = [];
     while ($dependency = $result->fetchObject()) {
@@ -1220,7 +1197,7 @@ class H5PDrupal implements \H5PFrameworkInterface {
     _drupal_flush_css_js();
 
     // Clear field view cache for ALL H5P content
-    \Drupal\Core\Cache\Cache::invalidateTags(['h5p_content']);
+    Cache::invalidateTags(['h5p_content']);
   }
 
   /**
@@ -1230,7 +1207,7 @@ class H5PDrupal implements \H5PFrameworkInterface {
    * @return int
    */
   public function getNumNotFiltered() {
-    return intval(db_query("SELECT COUNT(id) FROM {h5p_content} WHERE filtered_parameters IS NULL AND library_id > 0")->fetchField());
+    return intval(\Drupal::database()->query("SELECT COUNT(id) FROM {h5p_content} WHERE filtered_parameters IS NULL AND library_id > 0")->fetchField());
   }
 
   /**
@@ -1238,14 +1215,14 @@ class H5PDrupal implements \H5PFrameworkInterface {
    */
   public function getNumContent($library_id, $skip = NULL) {
     $skip_query = empty($skip) ? '' : " AND id NOT IN ($skip)";
-    return intval(db_query('SELECT COUNT(id) FROM {h5p_content} WHERE library_id = :id' . $skip_query, [':id' => $library_id])->fetchField());
+    return intval(\Drupal::database()->query('SELECT COUNT(id) FROM {h5p_content} WHERE library_id = :id' . $skip_query, [':id' => $library_id])->fetchField());
   }
 
   /**
    * Implements isContentSlugAvailable
    */
   public function isContentSlugAvailable($slug) {
-    return !db_query('SELECT slug FROM {h5p_content} WHERE slug = :slug', [':slug' => $slug])->fetchField();
+    return !\Drupal::database()->query('SELECT slug FROM {h5p_content} WHERE slug = :slug', [':slug' => $slug])->fetchField();
   }
 
   /**
@@ -1269,7 +1246,7 @@ class H5PDrupal implements \H5PFrameworkInterface {
   /**
    * Implements hasPermission
    *
-   * @param H5PPermission $permission
+   * @param \H5PPermission $permission
    * @param boolean $canUpdateEntity
    * @return bool
    */
@@ -1315,12 +1292,12 @@ class H5PDrupal implements \H5PFrameworkInterface {
    */
   public function replaceContentTypeCache($contentTypeCache) {
     // Replace existing cache
-    db_delete('h5p_libraries_hub_cache')
+    \Drupal::database()->delete('h5p_libraries_hub_cache')
       ->execute();
     foreach ($contentTypeCache->contentTypes as $ct) {
       $created_at = new \DateTime($ct->createdAt);
       $updated_at = new \DateTime($ct->updatedAt);
-      db_insert('h5p_libraries_hub_cache')
+      \Drupal::database()->insert('h5p_libraries_hub_cache')
         ->fields(array(
           'machine_name' => $ct->id,
           'major_version' => $ct->version->major,
@@ -1352,7 +1329,7 @@ class H5PDrupal implements \H5PFrameworkInterface {
    * Implements loadAddons
    */
   public function loadAddons() {
-    $result = db_query("SELECT l1.library_id, l1.machine_name, l1.major_version, l1.minor_version, l1.patch_version, l1.add_to, l1.preloaded_js, l1.preloaded_css
+    $result = \Drupal::database()->query("SELECT l1.library_id, l1.machine_name, l1.major_version, l1.minor_version, l1.patch_version, l1.add_to, l1.preloaded_js, l1.preloaded_css
                           FROM {h5p_libraries} l1
                      LEFT JOIN {h5p_libraries} l2 ON l1.machine_name = l2.machine_name AND
                                                      (l1.major_version < l2.major_version OR
@@ -1382,18 +1359,15 @@ class H5PDrupal implements \H5PFrameworkInterface {
    * Implements libraryHasUpgrade
    */
   public function libraryHasUpgrade($library) {
-    return !!db_query(
-      "SELECT library_id
+    return !!\Drupal::database()->query("SELECT library_id
          FROM {h5p_libraries}
         WHERE machine_name = :name
           AND (major_version > :major
            OR (major_version = :major AND minor_version > :minor))
-        LIMIT 1",
-      array(
-        ':name' => $library['machineName'],
-        ':major' => $library['majorVersion'],
-        ':minor' => $library['minorVersion']
-      )
-    )->fetchField();
+        LIMIT 1", array(
+      ':name' => $library['machineName'],
+      ':major' => $library['majorVersion'],
+      ':minor' => $library['minorVersion']
+    ))->fetchField();
   }
 }
diff --git a/src/H5PDrupal/H5PEvent.php b/src/H5PDrupal/H5PEvent.php
index 42c4be61..fa33d4b4 100644
--- a/src/H5PDrupal/H5PEvent.php
+++ b/src/H5PDrupal/H5PEvent.php
@@ -28,7 +28,7 @@ class H5PEvent extends H5PEventBase {
     $data['user_id'] = \Drupal::currentUser()->id();
 
     // Insert into DB
-    $this->id = db_insert('h5p_events')
+    $this->id = \Drupal::database()->insert('h5p_events')
       ->fields($data)
       ->execute();
 
@@ -44,21 +44,20 @@ class H5PEvent extends H5PEventBase {
     $type = $this->type . ' ' . $this->sub_type;
 
     // Verify if counter exists
-    $current_num = db_query(
-        "SELECT num
+    $current_num = \Drupal::database()->query("SELECT num
            FROM {h5p_counters}
           WHERE type = :type
             AND library_name = :library_name
             AND library_version = :library_version
         ", array(
-          ':type' => $type,
-          ':library_name' => $this->library_name,
-          ':library_version' => $this->library_version
-        ))->fetchField();
+      ':type' => $type,
+      ':library_name' => $this->library_name,
+      ':library_version' => $this->library_version
+    ))->fetchField();
 
     if ($current_num === FALSE) {
       // Insert new counter
-      db_insert('h5p_counters')
+      \Drupal::database()->insert('h5p_counters')
           ->fields(array(
             'type' => $type,
             'library_name' => $this->library_name,
@@ -69,17 +68,16 @@ class H5PEvent extends H5PEventBase {
     }
     else {
      // Update counter with num+1
-     db_query(
-         "UPDATE {h5p_counters}
+     \Drupal::database()->query("UPDATE {h5p_counters}
              SET num = num + 1
            WHERE type = :type
              AND library_name = :library_name
              AND library_version = :library_version
          ", array(
-           ':type' => $type,
-           ':library_name' => $this->library_name,
-           ':library_version' => $this->library_version
-         ));
+       ':type' => $type,
+       ':library_name' => $this->library_name,
+       ':library_version' => $this->library_version
+     ));
     }
   }
 }
diff --git a/src/Plugin/Field/FieldFormatter/H5PDefaultFormatter.php b/src/Plugin/Field/FieldFormatter/H5PDefaultFormatter.php
index aebf0b94..8ba4b762 100644
--- a/src/Plugin/Field/FieldFormatter/H5PDefaultFormatter.php
+++ b/src/Plugin/Field/FieldFormatter/H5PDefaultFormatter.php
@@ -2,7 +2,6 @@
 
 namespace Drupal\h5p\Plugin\Field\FieldFormatter;
 
-use Drupal\Core\Asset\AssetCollectionOptimizerInterface;
 use Drupal\Core\Field\FormatterBase;
 use Drupal\Core\Field\FieldItemListInterface;
 use Drupal\h5p\H5PDrupal\H5PDrupal;
diff --git a/src/Plugin/Field/FieldWidget/H5PUploadWidget.php b/src/Plugin/Field/FieldWidget/H5PUploadWidget.php
index d34a9d71..3549ff2e 100644
--- a/src/Plugin/Field/FieldWidget/H5PUploadWidget.php
+++ b/src/Plugin/Field/FieldWidget/H5PUploadWidget.php
@@ -2,6 +2,7 @@
 
 namespace Drupal\h5p\Plugin\Field\FieldWidget;
 
+use Drupal\Core\File\FileSystemInterface;
 use Drupal\h5p\Plugin\Field\H5PWidgetBase;
 use Drupal\Core\Field\FieldItemListInterface;
 use Drupal\Core\Form\FormStateInterface;
@@ -66,7 +67,7 @@ class H5PUploadWidget extends H5PWidgetBase {
     $interface = H5PDrupal::getInstance('interface', $file_field);
     $h5p_path = $interface->getOption('default_path', 'h5p');
     $temporary_file_path = "public://{$h5p_path}/temp/" . uniqid('h5p-');
-    file_prepare_directory($temporary_file_path, FILE_CREATE_DIRECTORY);
+    \Drupal::service('file_system')->prepareDirectory($temporary_file_path, FileSystemInterface::CREATE_DIRECTORY);
 
     // Validate file
     $files = file_save_upload($file_field, $validators, $temporary_file_path);
