Construction of a handle should probably be made into a separate function, to avoid having the same code in multiple places.