converted i to j loop