TS-2891: Fix subtle casting bug that prevented Stat ID from being looked up correctly.
This commit also makes another cast less ambiguous and fixes a poorly handled error
condition.