Factored some code into a function