Code for returning and registering handles