Fix array subscript above array bounds error