Simplify replaceAllTypeVars and handle some more edge cases