Refactor with tests