Fixed race conditions; now passes all tests