Factored out some duplicate code.