Working version of the code.  Decided to use a handler context to deal with issues of freeing the current context which seemed to cause segfaults.