add support for locking and fix a few bugs