Fix: Lock file before writing