handle panic and try to cleanup