Such type checking should hopefully catch more bugs. This commit also fixes some existing type errors.