Added explicit size check to loop