Copied some functions from DNest3