Fixed bug in buffer flushing