refactor code, remove duplicate code