Better clean up resources when detaching from process