tweak locking and add simple test.
Merge pull request #180 from tyro89/master

tweak locking and add simple test.