fixed segfault when cleaning up