Move bitstream write to a separate function, and add assertions about the part which should not write to bitstream.