Smarter arrangement of BufferedWriter so that it can be properly closed
when main() is done