Finished rewriting to new type system (excluding validation).

Added basic test for type of type detection.