Refactor and strengthen Def type checking.