Added error catching to permutation routine