Fixing an ArrayIndexOutofBounds exception