lock/unlock to avoid race conditions