Rewrite entire program to use StateT

Note: This isn't as bad as it sounds. All the transformations involved
were quite mechanical, and precisely none of the behavior changed.