fix array out of bounds problem