fix some array out of bounds conditions