Added directory check, creates the directory if it does not already exists.