Fix seg fault
Fix close logic