Add junit tests
Improve lock management
Add junit tests
Code optimization