Replace foreach with map