Fixed bug with post-loop counter overwritten