refactored typechecker