Refactor and add more tests