Switched to S_ISDIR for portable check if filesystem entry is directory....

... Automatically remove trailing slashes from path.