edit distance algorithm and better result filtering