Refactored loop for more accurate results