Move $i outside of loop