refactor and add tests to tailing