fixing some race conditions