move repeated code to a new function