Fixed some segfaults on close