fixed crash on termination
proper termination