Use StateT monad instead of IORefs everywhere