Use operator bool() instead of is_valid().