Fixed index error in writer