fixed several serious bugs; added restore_duplicates()