Added duplicates error-trapping. Fixed other bugs