fix array index and undefined vars