Encapsulate type stuff

No new functionality, but I cleaned up/simplified the type stuff so it should make it a lot easier to add types and debug.
Why did you need to do this?