Unlock before leaving function