Mod: Working algorithm. Does not filter out duplicates.