Clean up the buffer while transacting
A little code refactoring