better error code handling+don't create file if failure