fix bug in counter (cnt + prg bug), minor refactoring