[Refactoring] Fixed possible index out of range exception.