Added lock mechanism. Better concurrency and some mongodb fixes.