Refactor 'buffer' logic