move existence of directories checking to end