working counting
partial refactoring
works