Combined common code into a function.