Improve test cleanup logic