Add error check to avoid reading past end of array.
Add error check to avoid reading past end of array.