refactor code to remove duplicate