WIP: fix scoping on count calculation