Memleak fix branch

A bunch of fixes for memleaks and a recursion limit error.
Also a few a few stack unwinding refactors.