use defined function instead of using duplicated code