Refactered functions, and added duplicity check