Fix race conditions and concurrency issues