Simplify the ComputeLinearIndex logic and fix a few bugs.