Refactor funtion gendata