Fixed race conditions in tests

All tests seem to consistently pass now. Fixed 3-5 different race
conditions in the tests that would cause them to fail occasionally.