Small fixes and rewrite write_bits