Fixed the index out of bounds error