Move preparing the handler to an api level function which can be used by 3rd
party libraries, if necessary.

There have been some nasty hacks surrounding getting the handler in the
correct state, hopefully this goes somewhat towards alleviating that problem.