Add state logging, new unit tests