more robust file handling