implementing safe apply