Some refactoring of the type checking code