use defined function instead of duplicate code