A slightly cleaner version of the code blob that produced my thesis.