Extract duplicated code into functions and use those functions.