Fix CWD and CDUP

This resolves the issues reported at:
- asylumfunk#35
- asylumfunk#36

These issues were discovered following the testing added at sstur#27

The code has been subsequently refactored and documented.

Please refer to individual commits for additional information.