Refactor  and test