Use charpos instead of pos to handle multibyte encoding