This maintains the type information which can be used for enforce
const-correctness and also for implementing more advanced ways of
handling the type information.
This converts the previous int handles to a strongly-typed wrapper
handle which will make the API more self-documenting and will ensure
that wrongly used handles are detected at compile time.
I already found a few mistakes with these changes which I also fixed
here.