fixed two threading race-conditions