Add bounds checking to prevent resource starvation