add a debug counter. the implementation is somehow dirty as it relies on globals, but somehow more clean actually