add locking, bug fixes