Fixed issues related to UTF-8 encoding and validity checking