Tweaked distinct-values function