fixed some race conditions