Moved cleanup code to finally to ensure this always happens