Add error handling (invalid types, keys and integer conversion)

Fixes issue #5