Added state persistence.
More refactoring.