Added a validity check

New function is_valid added with parameter, check_validity true by
default