corrected a couple of race conditions