Cleaned up and improved some of the template type parameter deduction codes.

Tests pass whereas there's still some mixin stuff to fix.