modification of K Thornton's HBK_permute program