Jon issue 289

Again here. Also make sure to set `str = tostring(str)` because otherwise the `str.length` check will fail below.