use counted raw-memory iterators to clean up buffer handling and improve exception safety