Put counter++ in front of nested if.