refactor into functions, add count