Added checks for directory existence