Term re-writing, with test cases.