Refactory, add integration test