Revert "Revert "fixed all the permutation functions""

This reverts commit ffdfdad171ed38f638154641c51a42e2a41d82e2.