Added locking implemenation and Api tests for it + some general clean up