Handle duplicates better - fixes #166