Improved checking for invalid tokens.