add logging; improve locking