refactored into bufferstream