partial refactor + functional looping