Added locking capability to allow multi-threaded usage