Refactor write_varint to be more readable