Improved the restore function