refactor twoterms constraint into something hopefully more efficient