fixed a bug where the final list still contains permutations, and made
the code simpler