changed global variable "i" to local variable