Restructured TS map computation

As close to final as it could get today.
Please leave a comment in this file or the README in `extern` where this code came from and if you modified it, briefly what you changed.