fixed document generation, added save on close