refactoring, better typechecking