I'd place the counter in the very last line of the function, to make sure the it's called after everything has finished well.