some refactoring; initial unit and integration tests