More work on the type-checking.

Should be pretty much complete. Only the bugs left :)