Rewrote dedupe_names

I realized that I was approaching it with special cases when I could
use a more general one.