ok finally atomic write works, some bug fixes