improved type errors, refactored main typechecking function, various fixes