Refactored GeneratePermutation method.