Add extra smartness to CWD detection
Merge branch 'cwd_fix'