refactor code & add integration tests.