move flattenTerms before AddKCell