refactor everything with more support from type system.
Merge branch 'more-rigorous-types'