Structure redesigned, onerror, terminate and close methods added. Nested functions can call both way now.