Assorted fixes and optimizations to type inference