Refactor and new tests