Basic save and restore working.
Bugfixes and code cleanup.