Replace IORef with MVar for better concurrency behaviour.