remove several possible (but unlikely) race conditions