Tidied up code for safety-checking user inputs.