Various bug fixes in writing (python3 compat, chunk division...)