Condense repeated code into a function.