you could just use .distinct in place of .toSet.toSeq (remove one set creation)