fixed issue where it was not correctly removing duplicates, and also cle...

...aned the code up a bit