Factored out common code.