Much faster code for permutations