Added error handling for invalid files.