Bug fixes for scenarios around multiple blocks and output file specified.

Fixing bug where multiple blocks in the source file cause the optional argument for output file to not work. 

Now writing to the output file only once.