fixed various race conditions