Changes to better engineer the locks.
Actually make it threadsafe (but slow)