Renamed iterator i to k to avoid conflict