use map.with_index instead of manual index incrementing