Add safeapply function