Added file handling and error checking.