deduplicate some code.