inStack[w]=false; is needed

inStack[w]=false; is needed, or there is a bug in your code...