Restructured TS map computation

As close to final as it could get today.
Put imports of non-optional packages at the top.