refactor for testability, add simple test