put duplicated code in functions.