Optimize test structure

Extracted from #414