Add some more integration tests and some refactoring