cleanup before writing