Added integration tests and refactored code