refactor and adding tests