Wrong tags count if multiple tags are being used

Here is the full tagcloud.php snippet that I call in header.php:

  <?php
    $tags = $pages->children()->visible()->pluck('tags', ',', true);
  ?>

  <ul class="tags">
    <?php foreach($tags as $tag):
        $count = $pages->children()->filterBy('tags', $tag)->count();
      ?>
    <li>
        <?= html($tag) ?>
        <strong><?= $count ?></strong>
    </li>
    <?php endforeach ?>
  </ul>