refactored check_file function