Fixed index out of range issue