Fix sub-terms and add tests