added the safty checks for directory creation