refactor some functions to do DRY