Write output to file if specified. Fixes #5