Fixed Handler cleanup order and potential segfault