Further optimizations. Removed the csv library, replaced it with direct BufferedWriter. Less idiomatic but a lot more efficient and faster.