Fix infinite loop when moving terms with parent terms