Refactor and testing