Term etc rewrite

It's working I think. Worked out the bugs, except a NPE in CombineLikeTerms; not sure how that happened. Merging anyway.