FIX: many potential inconsistencies and potential crashes
PERF: io performance improvement
ADDED: tests