Main state is now an atom and minor refactoring.