refactor and add new tests