refactor to more functional style