correctly handle file errors