Fix a bunch of type system issues