fix type variable scoping bug