Reuse code for writing to buffer
Fixed a bug in writing buffer to console output