refactored permutation