refactor to allow tests and inject config