Better error checking for 'isvalid' function.