refactor & tidy-up codes in domain.ml