Fix some dubious thread/locking behaviour.