Refactor and more tests