Improved version of my program, checks for illegal characters.