Code review: add formal type annotations. Fix a bunch of older incorrect
type annotations while I'm at it.