Adding error handling. Change file and directory existing test code