This code is duplicated from data_term.cpp. It should ideally be in one place.