Fixed bug in lock() and unlock()