Added more checks
Extracted the file check into its own function
Extracted the file check into its own function