WIP: Saving

Various bug fixes, refactoring, and support for saving.