Fixed race condition with directory creation

- Acquire lock before doing anything with writing back data.
- Also adjusted threshold for detecting erratic behavior.