add check before writing files