Added additional safeguards for profile writing.