Remove lots of dead code

* Remove lots of dead code.
* Add check for datatype overflows.
* Use native datatype indices instead of fixed 16bit/32bit ones.