Add clean-up guards to manage references in some places