Refactoring and more integration testing