updated to handle duplicates and switching views