Updated explode function to remove duplicates