reworked (and improved) type inference for functions