Determine loop boundaries