Refactor SQL buffering