Refactor and test