Fixed array out of bounds issue