Move duplicate code into generic functions.