integration test added. Reorganized and refactored code