refactor dataset function