Error checking to avoid 'invalid byte sequence'.