Smart buffer closing. Mirror of repo by Orphée Lafond-Lummis.