Cleanup in main, implements testwriting to file