Added support for buffering data instead of writing to file at every loop