Factored code for clarity