wrote the counter array, see comments for details