refactor and add testing