diff --git a/captcha.inc b/captcha.inc
index f4f34fd..d305ac0 100755
--- a/captcha.inc
+++ b/captcha.inc
@@ -84,7 +84,7 @@ function _captcha_generate_captcha_session($form_id = NULL, $status = CAPTCHA_ST
 
   // Insert an entry and thankfully receive the value
   // of the autoincrement field 'csid'.
-  $captcha_sid = db_insert('captcha_sessions')
+  $captcha_sid = \Drupal::database()->insert('captcha_sessions')
     ->fields([
       'uid' => $user->id(),
       'sid' => session_id(),
@@ -108,7 +108,7 @@ function _captcha_generate_captcha_session($form_id = NULL, $status = CAPTCHA_ST
  *   The new solution to associate with the given CAPTCHA session.
  */
 function _captcha_update_captcha_session($captcha_sid, $solution) {
-  db_update('captcha_sessions')
+  \Drupal::database()->update('captcha_sessions')
     ->condition('csid', $captcha_sid)
     ->fields([
       'timestamp' => REQUEST_TIME,
@@ -134,7 +134,7 @@ function _captcha_required_for_user($captcha_sid, $form_id) {
   }
 
   // Get the status of the current CAPTCHA session.
-  $captcha_session_status = db_query('SELECT status FROM {captcha_sessions} WHERE csid = :csid', [':csid' => $captcha_sid])->fetchField();
+  $captcha_session_status = \Drupal::database()->query('SELECT status FROM {captcha_sessions} WHERE csid = :csid', [':csid' => $captcha_sid])->fetchField();
   // Second check: if the current session is already
   // solved: omit further CAPTCHAs.
   if ($captcha_session_status == CAPTCHA_STATUS_SOLVED) {
diff --git a/captcha.module b/captcha.module
index fb940e9..addb1ca 100755
--- a/captcha.module
+++ b/captcha.module
@@ -16,6 +16,7 @@ use Drupal\Core\Form\FormStateInterface;
 use Drupal\Core\Link;
 use Drupal\Core\Routing\RouteMatchInterface;
 use Drupal\Core\Url;
+use Drupal\Core\Render\Element;
 
 /**
  * Constants for CAPTCHA persistence.
@@ -52,8 +53,8 @@ function captcha_help($route_name, RouteMatchInterface $route_match) {
       $output = '<h3>' . t('About') . '</h3>';
       $output .= '<p>' . t('"CAPTCHA" is an acronym for "Completely Automated Public Turing test to tell Computers and Humans Apart". It is typically a challenge-response test to determine whether the user is human. The CAPTCHA module is a tool to fight automated submission by malicious users (spamming) of for example comments forms, user registration forms, guestbook forms, etc. You can extend the desired forms with an additional challenge, which should be easy for a human to solve correctly, but hard enough to keep automated scripts and spam bots out.') . '</p>';
       $output .= '<p>' . t('Note that the CAPTCHA module interacts with page caching (see <a href=":performancesettings">performance settings</a>). Because the challenge should be unique for each generated form, the caching of the page it appears on is prevented. Make sure that these forms do not appear on too many pages or you will lose much caching efficiency. For example, if you put a CAPTCHA on the user login block, which typically appears on each page for anonymous visitors, caching will practically be disabled. The comment submission forms are another example. In this case you should set the <em>Location of comment submission form</em> to <em>Display on separate page</em> in the comment settings of the relevant <a href=":contenttypes">content types</a> for better caching efficiency.', [
-          ':performancesettings' => \Drupal::url('system.performance_settings'),
-          ':contenttypes' => \Drupal::url('entity.node_type.collection'),
+          ':performancesettings' => Url::fromRoute('system.performance_settings'),
+          ':contenttypes' => Url::fromRoute('entity.node_type.collection'),
         ]) . '</p>';
       $output .= '<p>' . t('CAPTCHA is a trademark of Carnegie Mellon University.') . '</p>';
       return ['#markup' => $output];
@@ -61,10 +62,10 @@ function captcha_help($route_name, RouteMatchInterface $route_match) {
     case 'captcha_settings':
       $output = '<p>' . t('A CAPTCHA can be added to virtually each Drupal form. Some default forms are already provided in the form list, but arbitrary forms can be easily added and managed when the option <em>Add CAPTCHA administration links to forms</em> is enabled.') . '</p>';
       $output .= '<p>' . t('Users with the <em>Skip CAPTCHA</em> <a href=":perm">permission</a> won\'t be offered a challenge. Be sure to grant this permission to the trusted users (e.g. site administrators). If you want to test a protected form, be sure to do it as a user without the <em>Skip CAPTCHA</em> permission (e.g. as anonymous user).', [
-          ':perm' => \Drupal::url('user.admin_permissions'),
+          ':perm' => Url::fromRoute('user.admin_permissions'),
         ]) . '</p>';
       $output .= '<p><b>' . t('Note that the CAPTCHA module disables <a href=":performancesettings">page caching</a> of pages that include a CAPTCHA challenge.', [
-          ':performancesettings' => \Drupal::url('system.performance_settings'),
+          ':performancesettings' => Url::fromRoute('system.performance_settings'),
         ]) . '</b></p>';
       return ['#markup' => $output];
   }
@@ -123,7 +124,7 @@ function template_preprocess_captcha(&$variables) {
       '#type' => 'details',
       '#title' => t('CAPTCHA'),
       '#description' => $element['#description'],
-      '#children' => drupal_render_children($element),
+      '#children' => Element::children($element),
       '#attributes' => [
         'class' => ['captcha'],
         'open' => [''],
@@ -356,7 +357,7 @@ function _captcha_get_posted_captcha_info(array $element, FormStateInterface $fo
       // and the database query should also be more efficient (because there is
       // an index on the CAPTCHA session ID).
       if ($posted_captcha_sid != NULL) {
-        $expected_captcha_token = db_query(
+        $expected_captcha_token = \Drupal::database()->query(
           "SELECT token FROM {captcha_sessions} WHERE csid = :csid",
           [':csid' => $posted_captcha_sid]
         )->fetchField();
@@ -366,7 +367,7 @@ function _captcha_get_posted_captcha_info(array $element, FormStateInterface $fo
           $posted_captcha_sid = NULL;
         }
         // Invalidate CAPTCHA token to avoid reuse.
-        db_update('captcha_sessions')
+        \Drupal::database()->update('captcha_sessions')
           ->fields(['token' => NULL])
           ->condition('csid', $posted_captcha_sid);
       }
@@ -400,7 +401,7 @@ function captcha_validate($element, FormStateInterface &$form_state) {
   // TODO: is this correct in all cases: see comments in previous revisions?
   $csid = $captcha_info['captcha_sid'];
 
-  $solution = db_query(
+  $solution = \Drupal::database()->query(
     'SELECT solution FROM {captcha_sessions} WHERE csid = :csid',
     [':csid' => $csid]
   )
@@ -431,7 +432,7 @@ function captcha_validate($element, FormStateInterface &$form_state) {
       // Correct answer.
       $_SESSION['captcha_success_form_ids'][$form_id] = $form_id;
       // Record success.
-      db_update('captcha_sessions')
+      \Drupal::database()->update('captcha_sessions')
         ->condition('csid', $csid)
         ->fields(['status' => CAPTCHA_STATUS_SOLVED])
         ->expression('attempts', 'attempts + 1')
@@ -439,7 +440,7 @@ function captcha_validate($element, FormStateInterface &$form_state) {
     }
     else {
       // Wrong answer.
-      db_update('captcha_sessions')
+      \Drupal::database()->update('captcha_sessions')
         ->condition('csid', $csid)
         ->expression('attempts', 'attempts + 1')
         ->execute();
diff --git a/image_captcha/src/Response/CaptchaImageResponse.php b/image_captcha/src/Response/CaptchaImageResponse.php
index d4c81db..d5b652f 100755
--- a/image_captcha/src/Response/CaptchaImageResponse.php
+++ b/image_captcha/src/Response/CaptchaImageResponse.php
@@ -51,7 +51,7 @@ class CaptchaImageResponse extends Response {
   public function prepare(Request $request) {
     $session_id = $request->get('session_id');
 
-    $code = db_query("SELECT solution FROM {captcha_sessions} WHERE csid = :csid",
+    $code = \Drupal::database()->query("SELECT solution FROM {captcha_sessions} WHERE csid = :csid",
       [':csid' => $session_id]
     )->fetchField();
 
@@ -374,7 +374,7 @@ class CaptchaImageResponse extends Response {
       // Get character dimensions for TrueType fonts.
       if ($font != 'BUILTIN') {
         putenv('GDFONTPATH=' . realpath('.'));
-        $bbox = imagettfbbox($font_size, 0, drupal_realpath($font), $character);
+        $bbox = imagettfbbox($font_size, 0, \Drupal::service('file_system')->realpath($font), $character);
         // In very rare cases with some versions of the GD library, the x-value
         // of the left side of the bounding box as returned by the first call of
         // imagettfbbox is corrupt (value -2147483648 = 0x80000000).
@@ -382,7 +382,7 @@ class CaptchaImageResponse extends Response {
         // can be used as workaround.
         // This issue is discussed at http://drupal.org/node/349218.
         if ($bbox[2] < 0) {
-          $bbox = imagettfbbox($font_size, 0, drupal_realpath($font), $character);
+          $bbox = imagettfbbox($font_size, 0, \Drupal::service('file_system')->realpath($font), $character);
         }
       }
       else {
@@ -438,7 +438,7 @@ class CaptchaImageResponse extends Response {
         imagestring($image, 5, $pos_x, $pos_y, $character, $color);
       }
       else {
-        imagettftext($image, $font_size, $angle, $pos_x, $pos_y, $color, drupal_realpath($font), $character);
+        imagettftext($image, $font_size, $angle, $pos_x, $pos_y, $color, \Drupal::service('file_system')->realpath($font), $character);
       }
     }
 
diff --git a/src/Element/Captcha.php b/src/Element/Captcha.php
index 2c76d8f..a74ce6a 100644
--- a/src/Element/Captcha.php
+++ b/src/Element/Captcha.php
@@ -74,7 +74,7 @@ class Captcha extends FormElement {
       // not reuse one from a posted form.
       $captcha_sid = _captcha_generate_captcha_session($this_form_id, CAPTCHA_STATUS_UNSOLVED);
       $captcha_token = md5(mt_rand());
-      db_update('captcha_sessions')
+      \Drupal::database()->update('captcha_sessions')
         ->fields(['token' => $captcha_token])
         ->condition('csid', $captcha_sid)
         ->execute();
@@ -96,7 +96,7 @@ class Captcha extends FormElement {
     //   ->fields(['token' => $captcha_token])
     //   ->condition('csid', $captcha_sid)
     //   ->execute();
-    $captcha_token = db_query("SELECT token FROM {captcha_sessions} WHERE csid = :csid", [':csid' => $captcha_sid])->fetchField();
+    $captcha_token = \Drupal::database()->query("SELECT token FROM {captcha_sessions} WHERE csid = :csid", [':csid' => $captcha_sid])->fetchField();
     $element['captcha_token'] = [
       '#type' => 'hidden',
       '#value' => $captcha_token,
diff --git a/src/Tests/CaptchaBaseWebTestCase.php b/src/Tests/CaptchaBaseWebTestCase.php
index 267938e..d4dd2d3 100755
--- a/src/Tests/CaptchaBaseWebTestCase.php
+++ b/src/Tests/CaptchaBaseWebTestCase.php
@@ -6,6 +6,7 @@ use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
 use Drupal\comment\Tests\CommentTestTrait;
 use Drupal\field\Entity\FieldConfig;
 use Drupal\simpletest\WebTestBase;
+use Drupal\Core\Session\AccountInterface;
 
 /**
  * The TODO list.
@@ -270,7 +271,7 @@ abstract class CaptchaBaseWebTestCase extends WebTestBase {
    */
   protected function allowCommentPostingForAnonymousVisitors() {
     // Enable anonymous comments.
-    user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, [
+    user_role_grant_permissions(AccountInterface::ANONYMOUS_ROLE, [
       'access comments',
       'post comments',
       'skip comment approval',
diff --git a/src/Tests/CaptchaTestCase.php b/src/Tests/CaptchaTestCase.php
index 924c1f7..4a8cdaf 100755
--- a/src/Tests/CaptchaTestCase.php
+++ b/src/Tests/CaptchaTestCase.php
@@ -2,6 +2,7 @@
 
 namespace Drupal\captcha\Tests;
 
+use Drupal\Core\Entity\Entity\EntityFormDisplay;
 use Drupal\Core\Field\FieldStorageDefinitionInterface;
 use Drupal\field\Entity\FieldConfig;
 use Drupal\field\Entity\FieldStorageConfig;
@@ -218,7 +219,7 @@ class CaptchaTestCase extends CaptchaBaseWebTestCase {
    */
   public function testAjaxFormRebuild() {
     // Setup captcha point for user edit form.
-    \Drupal::entityManager()->getStorage('captcha_point')->create([
+    \Drupal::entityTypeManager()->getStorage('captcha_point')->create([
       'id' => 'user_form',
       'formId' => 'user_form',
       'status' => TRUE,
@@ -237,7 +238,7 @@ class CaptchaTestCase extends CaptchaBaseWebTestCase {
       'field_storage' => $field_storage_config,
       'bundle' => 'user',
     ])->save();
-    entity_get_form_display('user', 'user', 'default')
+    EntityFormDisplay::load('user.user.default')
       ->setComponent('field_texts', [
         'type' => 'string_textfield',
         'weight' => 10,
