fixing array out of bound issue