A bunch of inference code, untested.