More optimization and removing duplicates