fixed issue with duplicates and performance by using group_by instead of distinct