refactor and add test