Factored the code.