Cleaned up the permutation-related routines and stripped out some dead code.