Important fix to array out of bounds error