Added MVar-based locking to try to fix multi-threaded crashes.