Simplify code and fixed memory corruption with utf8 decoding logic