changed i to volatile for correctness