Added locks around file writes