Fix previous merge of 'improve-locking' branch

I accidentally merged the first version of the 'improve-locking' branch.
Add changes to the 'v2' branch as separate commit.
6 files changed