Stateful tests and bug fixes