Various fixes (race conditions,bugs) and redundant types cleanup

Should be an `RLock` since multiple parallel writes can happen. This is to guard reads to `w.flush`