Fix possible StringIndexOutOfBoundsException