replace loops with mapcat