Completed gen_data function