Sorted out some filtering to remove duplicates from the query to the db