Rejig logic so that we can deal with invalid inputs.