Put duplicated code into a function