Simplify safeWrite()
Fix merge from master