Move `end()` logic to `Buffer` and simplify its logic