Added exception handling and directory check.