Use a single lock to avoid race conditions