Replace loop by .map.collect