Factored out duplicate code.