Move some functions to kdm_with_metadata