implemented flatmap