New version of apply-sub-to-term which works for restrictions. This seems to have uncovered other issues though.
Fixed issue where a retry of the dosync block would result in nonsense terms.