Rearranged generate_data()