Improve unmasking and UTF-8 validation code