small refactor for permutation code