move buffering to general purpose helper