Fix #72

Reset buffer idx on flush and be sure we're within buffer range.