Fix threading issue.

Also improve dupe check.