Added support for return termination
Some memory leak fixes