Add missing header
Remove lots of dead code
Add check for datatype overflows
use native datatype indices instead of fixed 16bit/32bit ones