diff --git a/src/scheduler.c b/src/scheduler.c
index 84626a19f1d257737b3093602c21d185f3ce1b35..a11b026146588cf7e3acbb68215fa69e5d960bbb 100644
--- a/src/scheduler.c
+++ b/src/scheduler.c
@@ -1034,8 +1034,15 @@ void scheduler_rewait_active_mapper(void *map_data, int num_elements,
   for (int ind = 0; ind < num_elements; ind++) {
     struct task *t = &s->tasks[tid[ind]];
 
-    if (t->skip || !((1 << t->type) & mask) || !((1 << t->subtype) & submask))
+    /* Ignore masked-out tasks. */
+    if (t->skip || !((1 << t->type) & mask) || !((1 << t->subtype) & submask)) {
+      t->skip = 1;
       continue;
+    }
+      
+    /* Increment the task's own wait counter for the enqueueing. */
+    atomic_inc(&t->wait);
+    t->tic = t->toc = 0;
 
     /* Skip sort tasks that have already been performed */
     if (t->type == task_type_sort && t->flags == 0) {
@@ -1083,18 +1090,6 @@ void scheduler_start(struct scheduler *s, unsigned int mask,
   s->mask = mask;
   s->submask = submask |= (1 << task_subtype_none);
 
-  /* Clear all the waits and times. */
-  for (int k = 0; k < s->active_count; k++) {
-    struct task *t = &s->tasks[s->tid_active[k]];
-    t->wait = 1;
-    t->tic = 0;
-    t->toc = 0;
-    if (((1 << t->type) & mask) == 0 || ((1 << t->subtype) & submask) == 0)
-      t->skip = 1;
-  }
-  /* message("clear took %.3f %s.", clocks_from_ticks(getticks() - tic),
-          clocks_getunit()); */
-
   /* Re-wait the tasks. */
   if (s->active_count > 1000) {
     threadpool_map(s->threadpool, scheduler_rewait_active_mapper, s->tid_active,
@@ -1296,6 +1291,8 @@ struct task *scheduler_done(struct scheduler *s, struct task *t) {
      they are ready. */
   for (int k = 0; k < t->nr_unlock_tasks; k++) {
     struct task *t2 = t->unlock_tasks[k];
+    if (t2->skip || !((1 << t2->type) & s->mask) || !((1 << t2->subtype) & s->submask))
+      continue;
 
     const int res = atomic_dec(&t2->wait);
     if (res < 1) {
diff --git a/src/scheduler.h b/src/scheduler.h
index 22c83ae3c551d4dc7e601e23f7a5301241bd1fa4..00d67a280b5483c3b41a2beed1dfcb728c79a73a 100644
--- a/src/scheduler.h
+++ b/src/scheduler.h
@@ -121,6 +121,7 @@ struct scheduler {
 __attribute__((always_inline)) INLINE static void scheduler_activate(
     struct scheduler *s, struct task *t) {
   if (atomic_cas(&t->skip, 1, 0)) {
+    t->wait = 0;
     int ind = atomic_inc(&s->active_count);
     s->tid_active[ind] = t - s->tasks;
   }