Use permute pkg

Closes most of the issues (but not all) raised in #31: this issue discusses details and remaining issues. There really are some remaining issues, but the branch was growing too big: it is now 20 commits long and changes 33 files.