Refactor, add tests