more correct lock/unlock function