rewrite WrapAroundCounter to not have undefined behavior