Fix get_safe_filename and safe_flatten