fixed an off-by-one file writing bug