Calculate loop count first