Duplicate handling code
Cleaned code and added test
Merge branch 'duplicate-handling' into develop