Factored out some code.