Fix out of bound access, and provide better tokenize api.