use onCleanup to make sure path is restored even if there is an error