Added more buffer cleanup logic