Added conditions to check for directories before creating them