Fix for index out of range exception