Use defer to handle resource cleanup.