Major refactoring of the type inference engine