better checking for directories and less nesting.